Learners
Learners

Reputation: 121

How to convert string date into timestamp

We are getting a date from an API in string and the format is RFC3339. I need to convert this into a timestamp in this format 20-MAR-22 12.00.00.00.000000000 AM

I tried using Timestamp.valueOf(objUs.getUserAcptDate) but here we are getting an exception.

I found by searching online RFC3339 is in this formate "2019-10-12T07:20:50.52Z" or it may be without T.

How can we convert the coming string from API into a timestamp and this is the formate which we are saving in DB 20-MAR-22 12.00.00.00.000000000 AM

Upvotes: 0

Views: 725

Answers (1)

Alex
Alex

Reputation: 5944

You could use DateTimeFormatter to parse/format date-time objects. There are many predefined formatters or you could build custom if required. For some advanced scenarios you could use DateTimeFormatterBuilder that provides many different options for parsing and formatting.

private static final DateTimeFormatter CUSTOM_DATE = DateTimeFormatter.ofPattern("dd-MMM-yy hh.m.ss.n a");
private static final DateTimeFormatter ISO_DATE = DateTimeFormatter.ISO_ZONED_DATE_TIME;


String input = "2019-10-12T07:20:50.52Z";
ZonedDateTime date = ZonedDateTime.parse(input, ISO_DATE);
String output = CUSTOM_DATE.format(date);

Upvotes: 1

Related Questions