This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 2 years ago.
How to parse this date string in java
"2020-06-12T00:00:00.000+00:00"
I tried the following code :
public static String convertToStandardDateString(String date) {
// 2020-06-12T00:00:00.000+00:00
String resDate = null;
try {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'X");
Date parsedDate = sdf.parse(date);
resDate = sdf.format(parsedDate);
} catch (Exception e) {
}
return resDate;
}
I am getting the ParsingException with the above code.
Stop using the deprecated java.util.Date and start using the java-8 date-time api. The date string you have represents date time with offset, so you can parse it directly into OffsetDateTime
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2020-06-12T00:00:00.000+00:00");
Related
This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Java - How to get the correct date format with LocalDate
(2 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 months ago.
Can I know how can convert one date format to another date format.
public static LocalDate localDateToAnotherLocalDate(String oldPattern, String newPattern, String input) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern(oldPattern);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern(newPattern);
LocalDate localDate = LocalDate.parse(input, oldFormat);
String output = localDate.format(newFormat);
System.out.println();
return getLocalDate(output, newPattern);
}
public static LocalDate getLocalDate(String date, String datePattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
return LocalDate.parse(date, formatter);
}
#Test
void getLocalDateToAnotherLocalDateTest() {
LocalDate localDate = DateUtil.localDateToAnotherLocalDate("yyyy-MM-dd", "dd-MM-yyyy", "2022-11-20");
System.out.println("localDate" + localDate.toString());
assertEquals("20-11-2022", localDate.toString());
}
A LocalDate object can only ever be printed in ISO8601 format (yyyy-MM-dd). In order to print the object in some other format, you need to format it and save the LocalDate as a string.
This question already has answers here:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017-11-23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
how to convert this string "2016-10-08T01:00:00-07:00" to date object in Java?
I want to know what is string format to use with SimpleDateFormat.
I have try
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = format.parse("2016-10-08T01:00:00-07:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Change your format from Z to X will work . Detail is here
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = format.parse("2016-10-08T01:00:00-07:00");
should be
Date date = format.parse("2016-10-08T01:00:00-0700");
The time zone should not have a colon delimiting hours and minutes.
You can try SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Best solution to refer # Change date format in a Java string
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I am trying to convert back a stored date/time string in the format of (MM dd, yyyy, HH:mm [AM/PM]), I followed this post to create the string date/time. My current code properly fetches the stored date time but when I try to parse it and display the Date object "d" I get a null output.
Java code:
public void loadExamData(Cursor cursor) {
exam.setText(cursor.getString(1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(cursor.getString(2));
} catch (ParseException ex) {
Logger.getLogger(edit_exam_form.class.getName()).log(Level.SEVERE, null, ex);
}
location.setText(cursor.getString(3));
}
First you have to parse the date string with the current pattern and then format it with your desired pattern.
For Example, if your current date string is like this [10/10/2016 14:30].
String curDate = "10/10/2016 14:30";
SimpleDateFormat curDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = curDateFormat.parse(curDate);
String desiredDate = desiredDateFormat.format(date);
// you can use this date string now
} catch (Exception e) {}
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
String timeString = "2016-02-18T20:15:37.421Z";
How do I convert this into Date object? I tried something like this
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeString = "2016-02-18T20:15:37.421Z";
Date date= dateFormat.parse(timeString);
That gives me an Unparseable date exception
We just need to change the date format to yyyy-MM-dd'T'HH:mm:ss.SSS, e.g.:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String timeString = "2016-02-18T20:15:37.421Z";
Date date;
try {
date = dateFormat.parse(timeString);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}