15

For our Russia tenant we are using "Europe/Moscow" timezone. But we are getting time with 1 hour ahead of the correct time.

Europe/Moscow is UTC+3 hours. But when I am print date formated with Europe/Moscow timezone getting 1 hour ahead of the correct time.

Thanks, Syamala.

Syamala
  • 173
  • 1
  • 1
  • 7
  • 3
    Please show some code. – Oliver Charlesworth Nov 13 '14 at 11:39
  • public static void main(final String[] args) { final TimeZone tzone = TimeZone.getDefault(); System.out.println("Default Time Zone ID - " + tzone.getID()); System.out.println("Default Time Zone Offset - (" + (tzone.getRawOffset() / 60 / 60 / 1000) + ") hour."); } I have set my machine timezone Mosco St.Petrs Burg timezone UTC+3 . But the time timezone offset printing with the above code :Default Time Zone ID - Europe/Moscow Default Time Zone Offset - (4) hour. – Syamala Nov 13 '14 at 15:51

4 Answers4

11

Java 8:

 System.out.println(LocalDateTime.now(ZoneId.of("Europe/Moscow"))
            .format(DateTimeFormatter.ofPattern("d.MM.yyyy 'um' HH:mm 'Uhr'")));
Valery Oster
  • 111
  • 1
  • 3
8

I notice that there was a legislative change to Russian time zone definitions in October 2014; chances are that your JRE simply doesn't know about it yet.

The Java Timezone Updater Utility should be able to fix this for you. As time passes, the updated time zone definitions should also eventually get included by default in newer JREs (although that admittedly doesn't help you right now).

Scott Dudley
  • 3,256
  • 1
  • 18
  • 30
5

you can use the joda-time api, version need greater than 2.5.because joda-time api update the timezone db after Russian time zone change from version 2.5.

Date timestamp = sdf.parse("11/17/2014 06:13:19");

TimeZone timezone = TimeZone.getTimeZone("Europe/Moscow");

DateTimeZone tz = DateTimeZone.forTimeZone(timezone);

DateTime jodaDateTime = new DateTime(timestamp, tz);

System.out.println(jodaDateTime.hourOfDay().get());
HugeShi
  • 51
  • 1
  • 2
0

try threeteen

import org.threeten.bp.format.*
import org.threeten.bp.*



 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = dtf.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(MILLISECONDS_VALUE_HERE), ZoneId.of("Europe/Moscow"));
JBoy
  • 5,398
  • 13
  • 61
  • 101