0

How to change 2018-12-24 12:00:00 +0800 to 2018-12-23 16:00:00 +0000 in Java?

private String currentDateandTime = new Date();

final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
//dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//fullFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

Date dateTest = dateFormat.parse(currentDateandTime);
currentDateandTime = fullFormat.format(dateTest);

currentDateanTime Result

2018-12-24 12:00:00 +0800
xingbin
  • 27,410
  • 9
  • 53
  • 103
Lester
  • 701
  • 1
  • 9
  • 47
  • 2
    Possible duplicate of [How to get local time of different time zones?](https://stackoverflow.com/questions/9156156/how-to-get-local-time-of-different-time-zones), specifically [this answer](https://stackoverflow.com/a/36773536/1813169) – MTCoster Dec 24 '18 at 04:15
  • What do you want to change? 1. print it in GMT (+0000) time zone? 2. Assume the time is given in GMT timezone (ie ignore +0800 timezone) and and print it? – Archit Dec 24 '18 at 04:34
  • 3
    `2018-12-24 12:00:00 +0800` and `2018-12-23 16:00:00 +0000` is not the same instant!!! – xingbin Dec 24 '18 at 04:40
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 27 '18 at 08:24
  • I cannot reproduce your issue. Instead I get “Type mismatch: cannot convert from Date to String”. – Ole V.V. Dec 27 '18 at 08:29
  • Possible duplicate of [Convert date string (EST) to Java Date (UTC)](https://stackoverflow.com/questions/12919067/convert-date-string-est-to-java-date-utc) and many other questions. – Ole V.V. Dec 27 '18 at 08:33

3 Answers3

0

need to set "GMT" timezone (+0000) for fullFormat in order to convert Default Timezone to use GMT Timezone :

Date currentDateandTime = new Date();

final DateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss XX", Locale.CHINA);
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
fullFormat.setTimeZone(gmtTime);
String currentDateandTimeInGMTFullFormat = fullFormat.format(currentDateandTime);
m fauzan abdi
  • 436
  • 5
  • 12
0

Well, your code starts with an issue.

private String currentDateandTime = new Date();

Assuming Date() is imported from here java.util.Date

This line should show compiler error

Anyways I assume you want to convert your LocalTimezone DateTime to GMT Date Time

Date currentDate = new Date();
System.out.println(currentDate);

final DateFormat gmtFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gmtFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String convertedDate = gmtFormatter.format(currentDate);
System.out.println(convertedDate);

OUTPUT:
Mon Dec 24 09:52:14 IST 2018
2018-12-24 04:22:14
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
0

If you only want to get the same instance in different time zones, you can do it this way:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-24 04:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned)); 

However, 2018-12-24 12:00:00 +0800 and 2018-12-23 16:00:00 +0000 is not the same instant. There are 12 hours interval between them, you need minus these 12 hours.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2018-12-24 12:00:00 +0800", formatter);
offsetDateTime = offsetDateTime.minusHours(12);
ZonedDateTime dateTimeInDesiredZoned = offsetDateTime.atZoneSameInstant(ZoneId.of("UTC"));
// 2018-12-23 16:00:00 +0000
System.out.println(formatter.format(dateTimeInDesiredZoned)); 
xingbin
  • 27,410
  • 9
  • 53
  • 103