1

I would like to convert a date given in the UTC format to a date in the CET format.

The problem is that I need to add or subtract hours accordingly.

Example:

Date = "2015-07-31 01:14:05"

I would like to convert it to German date (adding two hours):

2015-07-31 03:14:05" 

My code:

private static Long convertDateFromUtcToCet(String publicationDate) {
    //"2015-07-31 01:14:05"

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
    Date date = null;
    try {
        date = simpleDateFormat.parse(publicationDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.setTime(date);
    Date givenDate = calendar.getTime();
    System.out.println("Original UTC date is: " + givenDate.toString());

    TimeZone timeZone = TimeZone.getTimeZone("CET");
    calendar.setTimeZone(timeZone);
    Date currentDate = calendar.getTime();
    System.out.println("CET date is: " + currentDate.toString());

    long milliseconds = calendar.getTimeInMillis();

    return milliseconds;
}

This prints:

Original UTC date is: Sat Jan 31 01:14:05 IST 2015
CET date is: Sat Jan 31 01:14:05 IST 2015
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Aviade
  • 2,057
  • 4
  • 27
  • 49
  • You don't parse the date as UTC, that's probably the issue –  May 09 '16 at 10:33
  • 1
    If you look closely, you can see from the output what went wrong: in the absence of parseable timezone information in the input string `SimpleDateFormat` will default to whatever timezone you're in. `IST` in this case (so you're either in Ireland or in India, which should serve as a warning about using three-letter timezone abbreviations). – biziclop May 09 '16 at 10:34
  • If you are using Java8, check out the new data/calendar syntax. The new API is much much easy to understand and easy to use. – Minh Kieu May 09 '16 at 10:36
  • This line `currentDate.toString()` prints a `Date` in your default timezone, because `Date` doesn't hold any timezone information internally, it is just a point in time (count of millis from the epoch) using some hardcoded predefined format. What you need, is not "converting" (there's nothing to convert, you work with the same timestamp), but "parsing". To output a `Date` in a given timezone, consider using `SimpleDateFormat`. – Alex Salauyou May 09 '16 at 10:57

2 Answers2

2

First of all, your pattern string is wrong. It's yyyy-MM-dd, not yyyy-mm-dd.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

To parse with a given time zone, set it with:

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dejvuth
  • 6,986
  • 3
  • 33
  • 36
1

Try using:

long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String format = "yyyy-mm-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);

// Convert Local Time to UTC (Works Fine)
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmtTime = new Date(sdf.format(localTime));
System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"
        + gmtTime.toString() + "," + gmtTime.getTime());


// Convert UTC to Local Time
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
        + fromGmt.toString() + "-" + fromGmt.getTime());
Eric
  • 6,563
  • 5
  • 42
  • 66
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53