Get correct local time in java (Calendar)
Asked Answered
A

4

6

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?

Armistead answered 5/3, 2013 at 7:58 Comment(2)
Some related reading material: infiniteundo.com/post/25326999628/…Wheresoever
It seems you are using the correct flags for hour values between 0-23. See here for more information: docs.oracle.com/javase/7/docs/api/java/text/… Is it possible your problem is somewhere lower down in your code? How is time used?Skye
H
4
  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.

Hower answered 5/3, 2013 at 8:11 Comment(0)
S
2

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

Stroller answered 5/3, 2013 at 8:2 Comment(4)
Hmm. This seems to be the same as what the OP is already doing.Skye
@Skye Not really.. take another look.Stroller
If you're avoiding saying that the construction of the date is the problem (for the second time), it would be helpful to mention that in your answer. Dunno why all the veiled language; this isn't Socratic Overflow. The OP is confused because he assumed that the formatting was the problem. Your formatting is exactly the same, besides punctuation.Skye
@Skye I agree, it's not Socratic Overflow. But you're lucky, see this philosophy.stackexchange.comStroller
S
1

I would guess you're getting time zones mixed up. Get Calendar with your system's default locale:

Calendar cal = Calendar.getInstance(Locale.getDefault());
Spaak answered 5/3, 2013 at 8:0 Comment(2)
I try this but i must wait afternoon. I will accept your answer if its OKArmistead
@AdamVarhegyi You can simply change time and check it now :)Stroller
M
0

You're using the HH flags. Try using the kk flags instead.

For example, kk:mm instead of HH:mm.

Mallet answered 10/8, 2015 at 15:46 Comment(3)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.Architecture
@shekharsuman I disagree. Although it is not correct, this is a perfectly valid, short answer.Skye
Mike, HH is supposed to return values from 0-23, which is what the OP wants. kk returns values 1-24. per docs.oracle.com/javase/7/docs/api/java/text/…Skye

© 2022 - 2024 — McMap. All rights reserved.