Java Date Conversion with Timezone [duplicate] - java

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

Related

Timestamp string to Date [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
How to convert the string to a date
In the string we have a timestamp (for example "2018-07-11T04:40:30Z"),
and I want to simply convert this String time to 11-07-2018 16:40:30
String Time = jresponse.getString("timestamp");
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = sf.parse(Time);
I tried many times this code and it doesn't work because I have always this format "2018-07-11T04:40:30Z" and not this 11-07-2018 16:40:30 why ?
If you want to parse dates like "2018-07-11T04:40:30Z"then you should change the format as "dd-MM-yyyy HH:mm:ss" will never work with that date. The correct format should be something like yyyy-MM-dd'T'HH:mm:ssZ.

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);

Java: how to handle a date with format like '2014-01-01T01:14:48.000+08:00' [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I got a date 2014-01-01T01:14:48.000+08:00 from a web server. Does anyone know how to parse this correctly? How to get its time offset?
String s="2014-01-01T01:14:48.000+08:00";
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(sdf.format(s));
You need something like this,
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSX");
System.out.println(sdf.parse(y));
The X is for timezone.

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

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.

Categories