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.
Related
This question already has answers here:
String to ZonedDateTime is changing format
(2 answers)
Closed 1 year ago.
I have used the below code to convert the string to the required format. But in the output seconds, microseconds are missing.
LocalDate date = LocalDat.parse(inputString, DateTimeFormatter.ISO_LOCAL_DATE);
date.atStartOfDay(ZooneId.of("America/New_York").toOffsetDateTime().toString();
Output based on the above code: '2021-01-13T00:00-4:00'
Required output: '2020-01-13T00:00:00.000-4:00'
You can further reformat it using the format() method.
LocalDate date = LocalDate.parse("2021-01-13", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date.atStartOfDay(ZoneId.of("America/New_York")).toOffsetDateTime().format(DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss:SSSZ"))));
see full list of available date formats here
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
This question already has answers here:
convert String in time to Time object without Date
(3 answers)
Closed 6 years ago.
I am trying to convert a 6 digits String to Time without the date with SimpleDateFormat but I am getting the date of 01.01.1970 after converting the time. How can I just get the time stored in the time variable without the date?
Code
String timeString = "004500";
SimpleDateFormat formater = new SimpleDateFormat("hhmmss");
Date time= formater.parse(timeString );
you can use LocalTime which is what you want. You can parse a standard date like this:
String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
LocalTime time - dateTime.toLocalTime();
If you only have a time to parse you can use LocalTime.parse() method.
Note that this is only available starting from Java 8. You can also add Joda Time as a dependency if you are not using Java 8 yet.
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.
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
This question already has answers here:
String to LocalDate
(5 answers)
Closed 8 years ago.
I have a String in the form "20140518". How to convert it into LocalDate object
I tried this
this.todayDate = new LocalDate(val);
System.out.println(todayDate.toString("yyyy-mm-dd"))
When I try dumping this to standard output it dumps like 20140518-junk-junk. That it dumps a garbage string . I thought it would dump like 2014-05-18.
Use MM that represents Month instead of mm that represents minutes.
Use LocalDate.parse() instead of new LocalDate() to construct the LocalDate object.
DateTimeFormatter format = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd");
LocalDate lDate = org.joda.time.LocalDate.parse("20140518", format);
System.out.println(lDate);
output:
2014-05-18
org.joda.time.LocalDate#toString() be default uses yyyy-MM-dd pattern.
You don't need to use todayDate.toString("yyyy-MM-dd").