I use this method to get local time:
Calendar cal = Calendar.getInstance();
String time= new SimpleDateFormat("dd MMM yyyy HH:mm:ss").format(cal.getTime());
My problem is that afternoon this method gives me back for example "11:15" but it is "23:15" already. I hope my desc is not confusing.
I want to get back afternoon values like: 12:MM , 13:MM, 14:MM ..etc goes to 23:MM. . .
What should i change?
now() now() method of a LocalDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return LocalDateTime based on system clock with default time-zone to obtain the current date-time.
The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.
now() now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on system clock with default time-zone to obtain the current date.
You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.
Are you sure that the time on the PC or whatever you are using to program is correct?
Try using the Gregorian calendar:
new GregorianCalendar().getTime()
Make sure you have these imports:
import java.util.Date;
import java.util.GregorianCalendar;
Hope that helps.
You can try something like that:
Date date=new Date();
System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm").format(date));
Output example:
2013.03.05 22:07
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With