transform ISO date from JSON to date Object in java [duplicate] - java

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 9 years ago.
I am getting this "createdAt":{"__type":"Date","iso":"2013-05-04T16:24:42.701Z"} as part of JSON from my backend. How can I convert it to Java Date object. in pseudo:
Date d = ISODateFromJSONToJavaDate(jsonObj);
progress:
is there any way to format iso type date String to java date?
Thanks!

If you are open to using a library - Joda Time might be a good candidate for handling this.
The other option is to build a SimpleDateFormat using a matching format string.

Related

Converting the timestamp from Facebook data after downloading messages in JSON format in Java [duplicate]

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Convert String in milliseconds to Date Object (JAVA) [duplicate]
(4 answers)
Converting Epoch time to date string
(10 answers)
Closed 4 years ago.
I was unable to find an answer to this problem, so once I figured it out, I felt posting the answer might help other people down the road.
The issue is when you have downloaded your messages from Facebook and you download them in JSON format, the timecode needs to be converted to a format that can be used in your Java program.
The timecode from Facebook is a 13 digit number that looks something like this: 1548410106047
The solution to the problem is:
You must first convert it to a Long datatype, then simply make a Timestamp out of it like this:
Long fbt = Long.parseLong(facebookTime);
Timestamp ts = new Timestamp(fbt);
Once you have it in a timestamp you can do anything with it such as:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, YYYY hh:mm a");
System.out.println(sdf.format(ts));
The output from above looks like this:
Friday, January 25, 2019 01:55 AM

How to convert format (2018-04-12T03:00:00.000Z) to Date from java.util package using Java [duplicate]

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Closed 4 years ago.
Hello there im receiveing from frontend a string who has this format (2018-04-12T03:00:00.000Z), so, i have to convert it to Date from Util package using Java.(java.util.Date)
Is there a good way to do this?
If you are using Java 8+, the easiest way is to use the java.time package to parse the date:
Date date = Date.from(Instant.parse("2018-04-12T03:00:00.000Z"));
String sDate1="2018-04-12T03:00:00.000Z";
Date date1=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(sDate1);
System.out.println(sDate1+"\t"+date1);

Return date instance java [duplicate]

This question already has answers here:
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Parse ISO timestamp using Java 8 java.time api (standard edition only)
(4 answers)
Closed 5 years ago.
I understand that if I want to convert a text into date with locale setting, I can use SimpleDateFormat constructor passing the pattern and locale. Then parse the text and format the date returned from parsing. What I am trying to do is return a date instance instead of string returned from SimpleDateFormat.format(date). Parsing it again will return date in english but I need a date instance in the locale like locale.russia. So basically if I input a string “16 nov 2016” I need Russian version of it as an instance of date not as a string.
Java 8 to rescue :
String date = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(new Locale("ru"))
.format(LocalDate.of(YYYY, MM, DD));
System.out.println(date);

get today's date and time as string [duplicate]

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API

Java Date Conversion with Timezone [duplicate]

This question already has answers here:
Convert Date/Time for given Timezone - java
(16 answers)
Closed 8 years ago.
I have user input date with TimeZone "GMT +5:30". But JVM is using "GMT +0:00". I want to convert Date from "GMT +5:30" to "GMT +0:00".
Thanks
Date in Java dont have a timezone. You need to try like this:
SimpleDateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("GMT+5.30"));
System.out.println(df.format(c.getTime()));
Also you can use the Joda-Time

Categories