Reputation: 137
I receive a date that represents a datetime in utc. Lets say: 21-Jun-2019 10:00
I'd like to convert this datetime to the timezone "Europe/Vienna" expecting: 21-Jun-2019 12:00
I do not understand, why my code below shows the same time for both
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
Calendar finishInViennaTime = Calendar.getInstance(europeVienna);
finishInViennaTime.setTime(utcFinish);
System.out.println(format.format(utcFinish));
System.out.println(format.format(finishInViennaTime.getTime()));
Output:
2019-06-21 00:00
2019-06-21 00:00
What would be the best java7 only (no joda, localdate pls) solution!? Thank you
EDIT: I also tried:
SimpleDateFormat formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm");
formatWithTimezone.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
SimpleDateFormat formatonly = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcDate = new Date(new Date().getYear(), Calendar.JUNE, 21);
System.out.println(formatonly.format(utcDate));
System.out.println(formatWithTimezone.format(utcDate));
Output:
2019-06-21 00:00
2019-06-21 00:00
SOLUTION
Thanks for all the solutions. In the end the problem was the default timezone. Here is my current solution (further feedback welcome!):
// Unfortunately this date has the wrong time zone (Local Time Zone),
// because Date assumes Local Time Zone the database stores timestamps
// in utc that's why I now convert to a datestring and reparse
Date finishTimeWrongTimeZone = new Date(new Date().getYear(), Calendar.JUNE, 21);
// in reality i call the db here like getFinishTime();
// get the plain date string without time shifting
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
String dateWithoutTimeZone = formatter.format(finishTimeWrongTimeZone);
// add the timezone to the formatter and reinterpret the datestring
// effectively adding the correct time zone the date should be in
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String finishTime = null;
try {
Date dateWithCorrectTimeZone = formatter.parse(dateWithoutTimeZone);
// Convert to expected local time zone (europe/vienna)
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
finishTime = formatter.format(dateWithCorrectTimeZone);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(finishTime);
Upvotes: 0
Views: 1145
Reputation: 3402
java.time
) solutionI have a version for the old API below like you asked, but for completeness, I will also provide the more modern solution. I recommend you to look into ThreeTen-Backport if updating Java is not an option:
ZonedDateTime zdt = LocalDateTime.of(
Year.now().getValue(), Month.JUNE, 21, 10, 0, 0
).atZone(ZoneOffset.UTC);
System.out.println(
zdt.withZoneSameInstant(ZoneId.of("Europe/Vienna"))
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"))
);
java.util
) solutionnew Date(...)
is deprecated, and you should not use it. If you really need to stick to the old API; you'll need to use Calendar
:
Calendar utcFinish = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
utcFinish.set(Calendar.MONTH, Calendar.JUNE);
utcFinish.set(Calendar.DATE, 21);
utcFinish.set(Calendar.HOUR_OF_DAY, 10);
utcFinish.set(Calendar.MINUTE, 0);
And then use a DateFormat
with the time zone that you actually wish to print it with:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish.getTime()));
Both solutions should output (at time of writing, in 2019):
2019-06-21 12:00
Upvotes: 1
Reputation: 51892
I use one formatter object and changes the time zone on it
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm"); format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dUtc = format.parse("21-06-2019 10:00");
System.out.println(dUtc);
TimeZone europeVienna = TimeZone.getTimeZone("europe/vienna");
format.setTimeZone(europeVienna);
String sVienna = format.format(dUtc);
System.out.println(sVienna);
Upvotes: 1
Reputation: 1735
Alter the Timezone before doing the format. The date will be "converted" accordingly, but as we already told you, this old coding style has a lot of flaws:
public static void main(String[] x) {
Date instant = new Date(new Date().getYear(), Calendar.JUNE, 21); // this call assumes the Timezone is your current default (system dependant).
DateFormat sdf = SimpleDateFormat.getDateTimeInstance();
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
System.out.println(sdf.format(instant)); //21 juin 2019 00:00:00
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Greenwich"));
System.out.println(sdf.format(instant)); // 20 juin 2019 22:00:00
}
Upvotes: 1
Reputation: 1
Please use SimpleDateFormat to convert timezone
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish));
Upvotes: 0