JTT511
JTT511

Reputation: 23

Using SimpleDateFormat but the hour value is 3 hours backwards

Im using SimpleDateFormat to get the current date and hour, the Date is working well but for unknown reasons the hour value is 3 hours backwards. for example if I will run the code on 19:40 the time value will be 16:40 and I don't know why. Would really appreciate some help.

String timeStamp = new SimpleDateFormat("M/d/yyyy&HH:mm").format(Calendar.getInstance().getTime());

Upvotes: 1

Views: 794

Answers (2)

Anonymous
Anonymous

Reputation: 86323

java.time and ThreeTenABP

java.time, the modern Java date and time API, gives a pretty natural way to control the time zone. For example:

    ZoneId zone = ZoneId.of("Asia/Istanbul");
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/uuuu&HH:mm");
    String timeStamp = ZonedDateTime.now(zone).format(formatter );
    System.out.println(timeStamp);

When I ran this snippet just now, the output was:

6/6/2019&19:53

I don’t know what your time zone is and trust you to specify the correct one. It matters.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

What went wrong in your code?

As others have said, your issue is a time zone issue. Apparently your SimpleDateFormat had its time zone set to UTC. When you’re not specifying time zone, it picks up its time zone from the JVM, which in turn usually picks it up from the operating system if not specified otherwise. That your JVM is using UTC is a standard practice, so not that much of a surprise.

Anyway, the datetime classes you were using, SimpleDateFormat and Calendar, are poorly designed, the former in particular notoriously troublesome. Fortunately both are long outdated. Instead use java.time, the modern Java date and time API.

I've just noticed that the phone hour is 3 hours backwards as well. Is there a way to change it?

Most likely the phone system clock is correct (maybe even updated automatically from a time server), but its time zone is set to UTC. There should be a way to set it to your time zone in the phone settings (then it will likely also be used by your JVM).

ISO 8601

As an aside your timestamp format is peculiar. I recommend that you use a standard format, best ISO 8601, and also that you include offset in the string so that even if the time zone was wrong, the time would still be unambiguous and correct. Two examples. First you may use UTC consciously:

    String timeStamp = Instant.now().toString();

2019-06-06T16:57:19.493599Z

The trailing Z means UTC.

If you want your own time zone:

    String timeStamp = OffsetDateTime.now(zone).toString();

2019-06-06T19:58:29.788376+03:00

Links

Upvotes: 1

Bohemian
Bohemian

Reputation: 425168

A Date does not have any timezone information; it's just a wrapper around a long that is assumed to be UTC epoch milliseconds. It's no coincidence that your timezone is 3 hours ahead of UTC and the formatted time is 3 hours out.

Although you can specify the timezone that SimpleDateFormat renders a Date in:

SimpleDateFormat format = new SimpleDateFormat("M/d/yyyy&HH:mm");
format.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));

Due to their many problems, Date and Calendar are largely deprecated.

Use LocalDateTime and DateTimeFormatter instead:

String timeStamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("M/d/yyyy&HH:mm"));

Upvotes: 0

Related Questions