Convert date string (EST) to Java Date (UTC)

15,617

Solution 1

Your date is getting converted right. Its just printing value in your default timezone format as java.util.Date is timezone independent. If you want timezone specific handling, please use java.util.Calendar.

Solution 2

As the correct accepted answer by Singh says, your Date actually is in UTC but its toString method confusingly applies the current default time zone while generating the string.

ISO 8601

Avoid such formats as 10/16/2012 12:06 PM for date-time values. When serializing to text, use the ISO 8601 formats defined as a standard for this very purpose.

java.time

I'm sorry if this seems to be an easy question I have a lot of trouble with Java dates

It's not you; it's the classes. The old legacy date-time classes were a valiant industry-leading effort at handling date-time. But they proved to be ill-conceived, poorly-designed, very confusing, and troublesome. Now supplanted by the java.time classes – a gigantic improvement.

Avoid this troublesome old java.util.Date class entirely. Instead use Instant in its place.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Get the current moment.

Instant instant = Instant.now();

You can convert a Date to its modern replacement by calling one of the new conversion methods added to the old date-time classes. Just call toInstant, quite easy.

Instant instant = myJavaUtilDate.toInstant(); 

The java.time classes use ISO 8601 formats by default when generating strings. Just call toString to get a clear representation of the value within the object.

String output = instant.toString();

2016-12-23T01:33:09.731Z

Parsing

To parse your input string, define a formatting pattern to match. The pattern codes are similar to that of SimpleDateFormat but not exactly the same. So be sure to study the doc carefully.

String input = "10/16/2012 12:06 PM" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu hh:mm a" );

Your input lacks any clue about offset-from-UTC or time zone. So we must parse as a LocalDateTime. Lacking any offset or zone, a LocalDateTime is only a vague idea about possible moments but does not represent a point on the timeline.

LocalDateTime ldt = LocalDateTime.parse( input , f );

ldt.toString(): 2012-10-16T12:06

The Question claims this was meant for “EST time zone”. So we need to apply a time zone, a ZoneId, to our LocalDateTime to get a ZonedDateTime.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

Perhaps by EST you meant the time zones used across much of the east coast of the United States and Canada. I will arbitrarily choose America/New_York.

ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = ldt.atZone( z );

zdt.toString(): 2012-10-16T12:06-04:00[America/New_York]

To get to UTC, simply extract a Instant. You can think of this conceptually as:

ZonedDateTime = ( Instant + ZoneId )

Instant instant = zdt.toInstant();

instant.toString(): 2012-10-16T16:06:00Z

Share:
15,617
user619804
Author by

user619804

Updated on June 04, 2022

Comments

  • user619804
    user619804 almost 2 years

    I need some advice on this java method. The intent of this method is to take a string that represents a date - this string was created from a date in the EST time zone - and convert it to a java Date object in the UTC time zone.

    private Date buildValidationDate(String dateString) throws ParseException {
        System.out.println("dateString " + dateString);
    
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyy hh:mm a");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));  
        dateFormat.setLenient(true);
        Date dt = dateFormat.parse(dateString);
    
        System.out.println("dt " + dt);
    
        return dt;
    }
    

    the problem I'm seeing is the value of dt seems to be off. For instance, if dateString is '10/16/2012 12:06 PM' - I'm expecting the value of dt (in UTC) to be something like 'Tuesday, October 16, 2012 4:06 PM'. Instead the value of dt is 'Tue Oct 16 07:06:00 CDT 2012'. This does not seem to be the correct UTC time.

    I appreciate any advice, I'm sorry if this seems to be an easy question I have a lot of trouble with Java dates. I'm not sure if I'm coding something incorrectly or if there is something wrong with my methodology. Thanks

  • Basil Bourque
    Basil Bourque almost 9 years
    Even better: Use the new java.time package.