-5

I need to get normal correct time in android application. But every time I run the app, I get wrong hours and wrong minutes. And it is not the incorrect TimeZone. Every time I run the app, the time is different! I've searched through questions here, but haven't found the answer for me. Nothing works with this... Why is it so problematic in java? In c#. for example, you can get time by just writting: var time = DateTime.Now. How can I get normal correct time? The time zone on my computer is set to UTC +02:00. (Ukraine, Kiev). My code:

Calendar curTime = new GregorianCalendar();
curTime.setTimeZone(TimeZone.getTimeZone("Ukraine/Kiev"));
DateFormat dateFormat = new SimpleDateFormat("HH:ss");
dateFormat.setCalendar(curTime);

String time = dateFormat.format(curTime.getTime());
Denis Yakovenko
  • 127
  • 2
  • 11
  • 1
    can you share some code?! – Booger Sep 25 '14 at 15:59
  • 5
    "Every time I run the app, the time is different!" Uh, what exactly are you expecting? – kviiri Sep 25 '14 at 16:01
  • What is the code that you use to get the Time in android ,, @Booger don't ask about the C# code ! – Omar HossamEldin Sep 25 '14 at 16:03
  • I've added my code into the question. Well, the thing is that, for example, now my time is 19:05. If i run the app, i get 16:54. Then, if i run it for the second time, i can get sth like 21:12 – Denis Yakovenko Sep 25 '14 at 16:06
  • http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html. You have hours and seconds set in your `dateFormat`. Try `HH:mm`. – Voicu Sep 25 '14 at 16:06
  • @Voicu, i've changed my code to `curTime.setTimeZone(TimeZone.getTimeZone("GMT+02:00")); DateFormat dateFormat = new SimpleDateFormat("HH:mm");` But it's still different from correct time. – Denis Yakovenko Sep 25 '14 at 16:15

2 Answers2

2

This answer has some of the time zones to use in your getTimeZone method.

Calendar curTime = new GregorianCalendar();
curTime.setTimeZone(TimeZone.getTimeZone("Europe/Kiev"));
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setCalendar(curTime);

String time = dateFormat.format(curTime.getTime());
System.out.println(time);

Output: 19:22

Community
  • 1
  • 1
Voicu
  • 16,921
  • 10
  • 60
  • 69
0

I hope this should work..time Zone is GMT+3 for Ukraine

  Date today = Calendar.getInstance().getTime();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
  sdf.setTimeZone(TimeZone.getTimeZone("GMT+3"));
  String date = sdf.format(today);

The output of above code ::

  2014-09-25-09.51.23
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37