Reputation: 9924
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?
Upvotes: 6
Views: 33247
Reputation: 116
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.
Upvotes: 4
Reputation: 1
You're using the HH
flags. Try using the kk
flags instead.
For example, kk:mm
instead of HH:mm
.
Upvotes: 0
Reputation: 96018
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
Upvotes: 2
Reputation: 35970
I would guess you're getting time zones mixed up. Get Calendar with your system's default locale:
Calendar cal = Calendar.getInstance(Locale.getDefault());
Upvotes: 1