0

I am having trouble with post object in Retrofit. What I need to do is making the TimeStamp variable to format of

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

However, my TimeStamp generated by my code

Timestamp timestamp = new Timestamp(new Date().getTime());

gives the format of 2021-07-11T16:53:45.604+00:00 which is converted to Jul 11, 2021 12:53:44 PM

I've looked into diverse documents, but cannot find a way to convert it to a different format.

Is there any way to change my TimeStamp variable format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z'?

Thanks!

dominono
  • 25
  • 3
  • Which Java version are you using? 1.7 or 1.8? – Vishal Jul 11 '21 at 17:28
  • @Vishal I am currently using JDK 9 – dominono Jul 12 '21 at 03:44
  • No, a `Timestamp` hasn’t got, as in cannot have a format. Does this answer your question? [Timestamp comes with .0 at the end](https://stackoverflow.com/questions/17545196/timestamp-comes-with-0-at-the-end) – Ole V.V. Jul 12 '21 at 05:06
  • I’m confused where the formats you mention are coming from. The code you have posted certainly does not give them. – Ole V.V. Jul 12 '21 at 05:08
  • 1
    I recommend you don’t use `Timestamp`. That class is poorly designed and long outdated. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 12 '21 at 05:09
  • I’m not sure the tags and mentions of Retrofit or MySQL are relevant here. – Basil Bourque Jul 12 '21 at 05:13
  • Retrofit may be, @BasilBourque. It’s nothing I know, but I seem to gather that it works through JSON. Possibly the OP can put an `Instant` instead if the `Timestamp` in the post object and maybe use https://github.com/FasterXML/jackson-modules-java8? – Ole V.V. Jul 12 '21 at 12:05

2 Answers2

1

tl;dr

Convert from troubled legacy class to modern class, then generate text in standard ISO 8601 format.

myTimestamp.toInstant().toString() 

Or capture the current moment in UTC.

Instant.now().toString()

Details

A java.sql.Timestamp object does not have have a “format”. The Timestamp class represents a moment, not text.

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

java.time.Instant

If you have a Timestamp object in hand, convert to its replacement, java.time.Instant. Look to new conversion methods added to the old classes.

Instant instant = myTimestamp.toInstant() ;

Capture the current moment as seen in UTC, with an offset from UTC of zero hours-minutes-seconds, using Instant.now().

Instant instant = Instant.now() ;

Generating text

Your desired format complies with the ISO 8601 standard. Those standard formats are used by default in the java.time classes when parsing/generating strings.

String output = instant.toString() ;  // Generate text in standard ISO 8601 format.

And parsing.

Instant instant = Instant.parse( "2021-07-11T16:53:45.604Z" ) ;

Z is not decoration

By the way, regarding your formatting pattern, yyyy-MM-dd'T'HH:mm:ss.SSS'Z', never put single quotes around the Z. The Z represents vital information, the offset-from-UTC. Your quotes kill that, turning it into a mere string literal.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If you are using Java 7, you can refer SimpleDateFormat to format the date as shown below

Timestamp timestamp = new Timestamp(new Date().getTime());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String format = simpleDateFormat.format(timestamp);
System.out.println(format);
Vishal
  • 674
  • 1
  • 7
  • 20
  • 1
    The OP is using Java 9. And the `SimpleDateFormat` class is a notorious troublemaker of a class and long outdated. – Ole V.V. Jul 12 '21 at 05:11
  • @Ole Yeah, the solution was intended for 1.7 – Vishal Jul 12 '21 at 08:57
  • For Java 6 and Java 7, use the back-port of the *java.time* classes found in the *ThreeTen-Backport* library. Never use the tragically flawed legacy classes `Timestamp`, `SimpleDateFormat`, `Date`, `Calendar`, etc. – Basil Bourque Sep 02 '22 at 14:52