In Java, you can get the current date time via following two classes:
1. Date
2.Calendar
Moreover, you can use SimpleDateFormat class to convert the date into user friendly format.
Please refer below code to understand it properly.
1. Date with SimpleDateFormat Class
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date);
System.out.println(currentDate);
2. Calendar with SimpleDateFormat Class
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime());
System.out.println(currentDate);
Also you can use the following code:
Calendar cal = null;
String currentDate = "";
cal = Calendar.getInstance();
currentDate = javaCalendar.get(Calendar.DATE) +
"/" + (javaCalendar.get(Calendar.MONTH) + 1) +
"/" + javaCalendar.get(Calendar.YEAR);
System.out.println(currentDate);
1. Date
2.Calendar
Moreover, you can use SimpleDateFormat class to convert the date into user friendly format.
Please refer below code to understand it properly.
1. Date with SimpleDateFormat Class
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date);
System.out.println(currentDate);
2. Calendar with SimpleDateFormat Class
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime());
System.out.println(currentDate);
Also you can use the following code:
Calendar cal = null;
String currentDate = "";
cal = Calendar.getInstance();
currentDate = javaCalendar.get(Calendar.DATE) +
"/" + (javaCalendar.get(Calendar.MONTH) + 1) +
"/" + javaCalendar.get(Calendar.YEAR);
System.out.println(currentDate);
0 comments:
Post a Comment