I want to convert this GMT time stamp to GMT+13:
2011-10-06 03:35:05
I have tried about 100 different combinations of DateFormat, TimeZone,
We can get the UTC/GMT time stamp from the given date.
/**
* Get the time stamp in GMT/UTC by passing the valid time (dd-MM-yyyy HH:mm:ss)
*/
public static long getGMTTimeStampFromDate(String datetime) {
long timeStamp = 0;
Date localTime = new Date();
String format = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat sdfLocalFormat = new SimpleDateFormat(format);
sdfLocalFormat.setTimeZone(TimeZone.getDefault());
try {
localTime = (Date) sdfLocalFormat.parse(datetime);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"),
Locale.getDefault());
TimeZone tz = cal.getTimeZone();
cal.setTime(localTime);
timeStamp = (localTime.getTime()/1000);
Log.d("GMT TimeStamp: ", " Date TimegmtTime: " + datetime
+ ", GMT TimeStamp : " + localTime.getTime());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return timeStamp;
}
It will return the UTC time based on passed date.
We can do reverse like UTC time stamp to current date and time(vice versa)
public static String getLocalTimeFromGMT(long gmtTimeStamp) {
try{
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.setTimeInMillis(gmtTimeStamp * 1000);
// calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
}catch (Exception e) {
}
return "";
}
I hope this will help others. Thanks!!
The java.util.Date/Calendar classes are a mess and should be avoided.
Update: The Joda-Time project is in maintenance mode. The team advises migration to the java.time classes.
Here's your answer using the Joda-Time 2.3 library. Very easy.
As noted in the example code, I suggest you use named time zones wherever possible so that your programming can handle Daylight Saving Time (DST) and other anomalies.
If you had placed a T
in the middle of your string instead of a space, you could skip the first two lines of code, dealing with a formatter to parse the string. The DateTime constructor can take a string in ISO 8601 format.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
// Parse string as a date-time in UTC (no time zone offset).
DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern( "yyyy-MM-dd' 'HH:mm:ss" );
DateTime dateTimeInUTC = formatter.withZoneUTC().parseDateTime( "2011-10-06 03:35:05" );
// Adjust for 13 hour offset from UTC/GMT.
DateTimeZone offsetThirteen = DateTimeZone.forOffsetHours( 13 );
DateTime thirteenDateTime = dateTimeInUTC.toDateTime( offsetThirteen );
// Hard-coded offsets should be avoided. Better to use a desired time zone for handling Daylight Saving Time (DST) and other anomalies.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html
DateTimeZone timeZoneTongatapu = DateTimeZone.forID( "Pacific/Tongatapu" );
DateTime tongatapuDateTime = dateTimeInUTC.toDateTime( timeZoneTongatapu );
Dump those values…
System.out.println( "dateTimeInUTC: " + dateTimeInUTC );
System.out.println( "thirteenDateTime: " + thirteenDateTime );
System.out.println( "tongatapuDateTime: " + tongatapuDateTime );
When run…
dateTimeInUTC: 2011-10-06T03:35:05.000Z
thirteenDateTime: 2011-10-06T16:35:05.000+13:00
tongatapuDateTime: 2011-10-06T16:35:05.000+13:00
display date and time for all timezones
import java.util.Calendar;
import java.util.TimeZone;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
DateFormat dateFormat = new SimpleDateFormat(ISO8601);
Calendar c = Calendar.getInstance();
String formattedTime;
for (String availableID : TimeZone.getAvailableIDs()) {
dateFormat.setTimeZone(TimeZone.getTimeZone(availableID));
formattedTime = dateFormat.format(c.getTime());
System.out.println(formattedTime + " " + availableID);
}
public Timestamp convertLocalTimeToServerDatetime(String dt,String timezone){
String clientDnT = dt ;// "2017-06-01 07:20:00";
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(clientDnT);
TimeZone tz = TimeZone.getTimeZone(timezone.trim()); // get time zone of user
sdf.setTimeZone(tz);
// Convert to servertime zone
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone tzInAmerica = TimeZone.getDefault();
sdf1.setTimeZone(tzInAmerica);
// assign date to date
String serverDate = sdf1.format(date);
// Convert to servertime zone to Timestamp
Date date2 = sdf.parse(serverDate);
Timestamp tsm = new Timestamp(date2.getTime());
return tsm;
}
catch(Exception e){
System.err.println(e);
}
return null;
}
Had a look about and I don't think theres a timezone in Java that is GMT + 13. So I think you have to use:
Calendar calendar = Calendar.getInstance();
//OR Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)+13);
Date d = calendar.getTime();
(If there is then change "GMT" to that Timezone and remove the 2nd line of code)
OR
SimpleDateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("GMT+13"));
System.out.println(df.format(c.getTime()));
If you want to set a specific time/date you can also use:
calendar.set(Calendar.DATE, 15);
calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.YEAR, 2011);
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 00);
I have try this code
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss Z");
Date datetime = new Date();
System.out.println("date "+sdf.format(datetime));
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("GMT "+ sdf.format(datetime));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+13"));
System.out.println("GMT+13 "+ sdf.format(datetime));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("utc "+sdf.format(datetime));
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+13"));
String newZealandTime = formatter.format(calendar.getTime());
System.out.println("using calendar "+newZealandTime);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and getting this result
date 06-10-2011 10:40:05 +0530
GMT 06-10-2011 05:10:05 +0000 // here getting 5:10:05
GMT+13 06-10-2011 06:10:05 +1300 // here getting 6:10:05
utc 06-10-2011 05:10:05 +0000
using calendar 06 Oct 2011 18:10:05 GMT+13:00