I have a Date & time "20140508063630" in (yyyyMMddHHmmss) format. I want to convert this Time into EST time zone. How can I do it? If there is any API for this conversion please let me know. Thanks in advance.
Eastern Time (ET): EST and EDT The Eastern Time Zone (ET) is an area 5 hours behind Greenwich Mean Time (GMT-5) during the winter months (referred to as Eastern Standard Time or EST) and 4 hours behind Greenwich Mean Time (GMT-4) during the summer months (referred to as Eastern Daylight Time or EDT).
GMT is five hours ahead of EST, or Eastern Standard Time, which is the time zone used by cities such as New York.
try {
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone estTime = TimeZone.getTimeZone("EST");
gmtFormat.setTimeZone(estTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("EST Time: " + gmtFormat.format(sdf.parse("20140508063630")));
} catch (ParseException e) {
e.printStackTrace();
}
Since you have tagged your question java-time, you do deserve a java.time
answer.
String dateAndTime = "20140508063630";
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
ZonedDateTime coralHarbourDateTime = LocalDateTime.parse(dateAndTime, parseFormatter)
.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(ZoneId.of("America/Coral_Harbour"));
System.out.println(coralHarbourDateTime);
This prints
2014-05-08T01:36:30-05:00[America/Coral_Harbour]
There are a number of things to note though:
java.time
seems to be coming to Android, but as of writing it is not on the majority of Android phones. Don’ t despair, though, get the ThreeTenABP and use the classes on Android.SimpleDateFormat
and TimeZone
classes used in the other answers are long outdated, so if your Android app is doing any work with dates and/or times and/or time zones, I do recommend ThreeTenABP and the modern classes as the programmer friendly and the futureproof way to go.2014-05-08T02:36:30-04:00[America/Montreal]
, for example, with time zone offset -4 instead of -5.Furhter link: How to use ThreeTenABP in Android Project
Following code for convert date from one timezone to another timezone with any date format
{ String parsedDate = null;
try {
SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
dbDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = dbUtcDateFormat.parse("20140508063630");
SimpleDateFormat userDateFormat = new SimpleDateFormat(yyyyMMddHHmmss);
userDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
parsedDate = userDateFormat.format(date);
} catch (Throwable t) {
logger.warn("Date Parse Faied : ", t);
}
return parsedDate;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With