Get correct local time in java (Calendar)

Adam Varhegyi picture Adam Varhegyi · Mar 5, 2013 · Viewed 30.8k times · Source

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?

Answer

Kersy picture Kersy · Mar 5, 2013
  1. Are you sure that the time on the PC or whatever you are using to program is correct?

  2. Try using the Gregorian calendar:

    new GregorianCalendar().getTime()
    
  3. Make sure you have these imports:

    import java.util.Date;
    import java.util.GregorianCalendar;
    

Hope that helps.