3

Server side code (server timezone is UTC):-

Date aDate = new Date();
java.sql.Timestamp aTimestamp = new java.sql.Timestamp(aDate.getTime());

Client side (Mobile app, timezone GMT +5:30):-

Hitting a service request which runs above code on server side

The issue is when i debugged on server, found following values :-

aDate.getTime() prints to -> 1470472883877 milliseconds i.e., Sat Aug 06 2016 14:11:23 GMT+0530

but

aTimestamp prints to ->  (java.sql.Timestamp) 2016-08-06 08:41:44.109

It's kinda weird, i've no idea what's going on in conversion !! please help

2
  • Can you please post the printing code? Commented Aug 6, 2016 at 8:59
  • System.out.println(aDate.getTime()) and System.out.println(aTimestamp) @johnnyaug Commented Aug 6, 2016 at 16:58

2 Answers 2

1

UTC and GMT are formats.
java.util.Date and java.sql.Timestamp are independent of the timezone. They store a long time in ms for representing their inner state. For information, Calendar is timezone aware.

So with Date or Timestamp, to differentiate GMT or UTC format in an output, you have to use a formater which outputs the date into string by being aware the timezone.

In your output : 2016-08-06 08:41:44.109, you don't use a formater which is aware of the timezone. It's probably the result of a toString() on the java.sql.Timestamp instance or something of similar.

What you consider as a conversion is not a conversion but a formatting since the timestamp stays the same between the two objects.

If you want to display in the UTC format, use the appropriate formater with a SimpleDateFormat for example :

SimpleDateFormat dt= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss z");
dt.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStringInUTC = dt.format(new Date(yourSqlTimestamp.getTime()));
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, you're right DateFormat and SimpleDateFormat are aware of timezones, but both of them returns a string value of the date. But i want the output as date
One solution is to parse the string to date using dateFormat.parse(strDate), but i don't want to use it as it increases the probability of java.text.ParseException: Unparseable date
I don't understand what you mean by " i want the output as date".A output should be a string, no? If what you want is having an date object with the good timestamp, you have already it since as explained what you call a conversion is not a conversion but a formatting.
If i don't understand your need, don't hesitate to explain what you mean by" i want the output as date"
0

The following is probably what you are looking for:

Calendar cal = Calendar.getInstance(Locale.GERMANY);  // use your locale here
Timestamp aTimestamp = new Timestamp(cal.getTimeInMillis());
System.out.println(aTimestamp);
System.out.println(cal.getTime());

And the output:

2016-08-06 19:12:54.613

Sat Aug 06 19:12:54 CEST 2016

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.