1

I'm currently working on timestamps that are converted from and to UTC. All articles that I found were based on conversion to and from String. Like this one:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));

But I wonder if there is a way to simply work with the Date instance/class or the calendar to convert the local Date into UTC and vice versa without converting it to String in between.

Community
  • 1
  • 1
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • see http://stackoverflow.com/questions/230126/how-to-handle-calendar-timezones-using-java – maasg Jun 13 '14 at 09:05
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). Solution here is simply: `Instant.now()` – Basil Bourque Feb 09 '18 at 01:08

4 Answers4

2

Read up on Joda-Time. That is a better API for such things than the java date and calendar classes

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
David Roussel
  • 5,788
  • 1
  • 30
  • 35
  • 1
    FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 09 '18 at 01:07
0

maybe this can help you:

Calendar.getInstance(java.util.TimeZone)

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
ZaoTaoBao
  • 2,567
  • 2
  • 20
  • 28
  • But how could you use two calendar instances (UTC and local) to convert between Date? – Matthias Jun 13 '14 at 08:38
  • take a look a 3er answ. great. http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – ZaoTaoBao Jun 13 '14 at 10:51
  • That post uses the SimpleDateTimeFormat which is actually a conversion from Date to String and vice versa. I just don't understand why date/time conversion are done by converting Date to String and then back to Date again. – Matthias Jun 13 '14 at 11:25
  • ok i feel like a big monkey I wanted to say de fourth post. the only that no uses simpledateformat. I hope you apologise me. – ZaoTaoBao Jun 13 '14 at 12:55
  • It's also using Java 8 while I'm still on Java 7. Anyway, the 2nd answer is a good one because it uses the offset as long. – Matthias Jun 13 '14 at 12:57
  • great! i take a look too. – ZaoTaoBao Jun 13 '14 at 13:01
0

java.until.Date does not have a timezone, so there's nothing to be converted. You only see a timezone when you format the date to a string explicitly, or implicitly by using its toString method. An implicit conversion uses the local default timezone.

Internally, Date stores the date/time as a long, representing milliseconds since midnight, Jan. 1, 1970, UTC.

So, if you format a date as a string, and then parse the string back to a date, you've changed nothing at all.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

So far, I could not find a perfect solution, so I had to stick to the conversion from Date to String and vice versa. Here's a little helper class that I wrote.

public class DateTimeHelper {

    public static final String MYSQL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final TimeZone timeZoneUTC = TimeZone.getTimeZone("UTC");

    private Date date = new Date();
    private final SimpleDateFormat format;

    public DateTimeHelper(String dateTimeFormat) {
        format = new SimpleDateFormat(dateTimeFormat, Locale.US);
    }

    public DateTimeHelper(String dateTimeFormat, String utcTimeString) {
        this(dateTimeFormat);

        try {
            format.setTimeZone(timeZoneUTC);
            Date utc = format.parse(utcTimeString);
            format.setTimeZone(TimeZone.getDefault());
            String local = format.format(utc);
            date = format.parse(local);
        } catch (ParseException e) {
            // nothing
        }
    }

    public Date getDate() {
        return date;
    }

    public Date toUtc() {

        String temp = toString();
        format.setTimeZone(timeZoneUTC);
        try {
            return format.parse(temp);
        } catch (ParseException e) {
            return date;
        }
    }

    @Override
    public String toString() {
        format.setTimeZone(TimeZone.getDefault());
        return format.format(date);
    }

    public String toUtcString() {
        format.setTimeZone(timeZoneUTC);
        return format.format(date);
    }
}

And another one that's easier to use:

public class MySqlDateTimeHelper extends DateTimeHelper {

    public MySqlDateTimeHelper() {
        super(DateTimeHelper.MYSQL_DATE_TIME_FORMAT);
    }

    public MySqlDateTimeHelper(String utcTimeString) {
        super(DateTimeHelper.MYSQL_DATE_TIME_FORMAT, utcTimeString);
    }

    public static String getCurrentTimestampUtc() {
        MySqlDateTimeHelper current = new MySqlDateTimeHelper();
        return current.toUtcString();
    }
}
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 09 '18 at 01:10