How to get the current date in java?
// in java file
public static String DateDemo() {
Date dNow = new Date();
//SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
// SimpleDateFormat ft = new SimpleDateFormat("M/d/yyyy hh:mm:ss a");
SimpleDateFormat ft = new SimpleDateFormat("d/M/yyyy");
System.out.println("Current Date: " + ft.format(dNow));
return ft.format(dNow);
}
or
public static String GetCurrentDateAndTime() {
int day, month, year;
int second, minute, hour, ampm;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
ampm = date.get(Calendar.AM_PM);
char ampm1 = (char) ampm;
System.out.println("Current date is " + day + "/" + (month + 1) + "/" + year);
System.out.println("Current time is " + hour + " : " + minute + " : " + second + ampm1);
return month + "/" + day + "/" + year + " " + +hour + ":" + minute + ":" + second + " " + ampm1;
}
// in java file
public static String DateDemo() {
Date dNow = new Date();
//SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
// SimpleDateFormat ft = new SimpleDateFormat("M/d/yyyy hh:mm:ss a");
SimpleDateFormat ft = new SimpleDateFormat("d/M/yyyy");
System.out.println("Current Date: " + ft.format(dNow));
return ft.format(dNow);
}
or
public static String GetCurrentDateAndTime() {
int day, month, year;
int second, minute, hour, ampm;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
ampm = date.get(Calendar.AM_PM);
char ampm1 = (char) ampm;
System.out.println("Current date is " + day + "/" + (month + 1) + "/" + year);
System.out.println("Current time is " + hour + " : " + minute + " : " + second + ampm1);
return month + "/" + day + "/" + year + " " + +hour + ":" + minute + ":" + second + " " + ampm1;
}
No comments:
Post a Comment