Date and SimpleDateFormat
This piece of code returns the date in the format year/month/day and the current time in the format hour/minute/second. HH in uppercase returns the 24-hour time system.
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Output
Date date = new Date();
System.out.println(format.format(date));
2015/07/22 18:36:43
Calendar and SimpleDateFormat
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Runtime:
Calendar calendar = Calendar.getInstance();
System.out.println(format.format(calendar.getTime()));
2015/07/22 18:39:24
Let's see the full code for using the Date and Calendar with the format predefined by the SimpleDateFormat and display the result:
import java.text.DateFormat;Runtime:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class getCurrentDate {
public static void main(String[] args) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
//get current date
Date date = new Date();
System.out.println(format.format(date));
//get current time
Calendar calendar = Calendar.getInstance();
System.out.println(format.format(calendar.getTime()));
}
}
2015-07-22 06:50:55 PM
2015-07-22 06:50:55 PM
The time format hh:mm:ss a returns the time system to 12 hours where the day is divided into two half-days of 12 hours each. AM for the period from midnight to noon and PM for the period from noon to midnight.
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...