Hi All I have a requirement where i need to check date with system date value but the date value i have in DB is in milliseconds so below are my code but the problem is the system date which i get display month value incorrect its display month value one month less .
Date now = new Date();
Long nowLong = now.getTime() / 1000;
Integer unixTime = nowLong.intValue();
Calendar cal;
cal = Calendar.getInstance();
cal.setTimeInMillis(unixTime * 1000L);
Integer day = cal.get(Calendar.DAY_OF_MONTH);
Integer month = cal.get(Calendar.MONTH);
Integer hour = cal.get(Calendar.HOUR);
Integer minute = cal.get(Calendar.MINUTE);
String amPm = (cal.get(Calendar.AM_PM) == Calendar.PM) ? "PM" : "AM";
Integer year = cal.get(Calendar.YEAR);
System.out.println("day :" + day);
System.out.println("month :" + month);
System.out.println("hour " + hour);
System.out.println("minute " + minute);
System.out.println("amPm " + amPm);
System.out.println("year " + year);
For this the output is day :3 month :4 hour 3 minute 13 amPm PM year 2016 but output is not correct . Can any one help me .
Integer
instead ofint
. In general, don't useDate
orCalendar
either. Use the date / time API (java.time
).