4
public static long getCurrentEpochTimeStamp(String timeStamp) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.0Z'");
    Date date = sdf.parse(timeStamp);
    return date.getTime();
}

This method returns epoch current timestamp, I need to convert this to UTC timezone.

Sunset Bloom
  • 111
  • 1
  • 2
  • 8

2 Answers2

6

Set time zone to your SimpleDateFormat object.

sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Date is always in UTC and you cannot convert it. The problem is how to parse string representation. If timeStamp contains timezone, then pattern "yyyy-MM-dd'T'HH:mm:ss'.0Z'" is incorrect because it does not parse timezone. It should be "yyyy-MM-dd'T'HH:mm:ssZ" if timezone is in RFC 822 format, use X instead of Z if timezone is in ISO 8601. See SimpleDateFormat API

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275