0

I am trying to generate current timestamp in GMT timezone but it is still getting generated in IST as my machine is set to IST. I am using this code. Please help!!

{      String timeZone = "GMT";
   Calendar gmtCalendar =          Calendar.getInstance(TimeZone.getTimeZone(timeZone)); 
   Date date = gmtCalendar.getTime();
   endTime = new Timestamp(date.getTime());
   String eTime = NLSUtil.getFormattedDate(AdfUtil.getClientLocale(), new    java.sql.Date(endTime.getTime()));
}
Mathias
  • 5,642
  • 2
  • 31
  • 46
Gabiesfat
  • 1
  • 1
  • 3

3 Answers3

1

i have no idea what NLSUtil is (can you elaborate?) but time formats have a timezone too (which defaults to the machine's timezone, IST in your case?)

Date toPrint = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(toPrint));
format.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(format.format(toPrint));

prints:

2015-01-16 10:18:34.604 GMT
2015-01-16 02:18:34.604 PST
radai
  • 23,949
  • 10
  • 71
  • 115
  • Thanks radai, sorry for pasting local api. SimpleDateFormat works, but I am trying to get either Timestamp or Date object out of it. Please let me know if it is feasible. – Gabiesfat Jan 16 '15 at 11:59
  • SimpleDateFormat has a parse() method which accepts a String as an argument and returns Date object - http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#parse-java.lang.String-java.text.ParsePosition- – radai Jan 16 '15 at 14:31
0
 SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));

//Local time zone   
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

//Time in GMT
System.out.println(dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));

Please Refer here

Community
  • 1
  • 1
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

Add TimeZone.setDefault(TimeZone.getTimeZone("UTC")); before getting the instance

shikjohari
  • 2,278
  • 11
  • 23