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();
}
Related
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");
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I'm trying to convert a date in dd-MM-yyyy format from YYYY-MM-dd hh:mm:ss.ms
in java using the below code but m unable to get the desired value
String dob = (new SimpleDateFormat("dd-MM-yyyy")).format(customerEntity.getDob().toString());
customerEntity.getDob().toString is providing me this value 1987-06-12 00:00:00.0
But when i'm parsing it to the String dob it produces 163-06-1987 as the output whereas i want the output like 12-06-1987 .
Any help will be appreciable, thanks well in advance
format method in SimpleDateFormat take a Date as argument and not a String
public static void main(String[] args) {
String dateStr = "29/12/2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Try parsing your string date into a Date first in format it is coming. Post that pass on that Date object to a format in the format you want your output.
As in below :
Date dob = (new SimpleDateFormat("yyyy-MM-dd")).parse("1987-06-12 00:00:00.0");
String dob1 = (new SimpleDateFormat("dd-MM-yyyy")).format(dob);
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:
How to parse date string to Date? [duplicate]
(6 answers)
Closed 9 years ago.
Hey guys I have the date "01.01.1000 AD"(SimpleDate) as String and dd.MM.yyyy G(SimpleFormat) and need to parse it into a Standard ISO-Date in the form 1995-12-31T23:59:59Z (yyyy-MM-dd'T'hh:mm:ss'Z')
my actual code is:
public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
Date date;
if (simpleFormat.equals("long")) {
date = new Date(Long.parseLong(simpleDate));
} else {
SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// or else testcase
// "1964-02-24" would
// result "1964-02-23"
date = df.parse(simpleDate);
}
return getISODate(date, isoFormat);
}
Does anyone have an idea how do I do that?
Try this:
String string = "01.01.1000 AD";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy GG");
Date date = dateFormat.parse(string);
The G in the date format stands for era.
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
This will hopefully help [tricky with standard jdk, but at least possible - and JSR 310 doesn't support this feature :-( ]:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy GG", Locale.US);
DateFormat iso = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = df.parse("01.01.1000 AD");
System.out.println(iso.format(d)); // year-of-era => 1000-01-01 (not iso!!!)
// now let us configure gregorian/julian date change right for ISO-8601
GregorianCalendar isoCalendar = new GregorianCalendar();
isoCalendar.setGregorianChange(new Date(Long.MIN_VALUE));
iso.setCalendar(isoCalendar);
System.out.println(iso.format(d)); // proleptic iso year: 1000-01-06
} catch (ParseException ex) {
ex.printStackTrace();
}
Something like this?
String date = "01.01.1000 AD";
SimpleDateFormat parserSDF = new SimpleDateFormat("dd.mm.yyyy GG");
System.out.println(parserSDF.parse(date));
Try it may be help:
public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
Date date;
if (simpleFormat.equals("long")) {
date = new Date(Long.parseLong(simpleDate));
} else {
SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
df.setTimeZone(TimeZone.getTimeZone("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
// or else testcase
// "1964-02-24" would
// result "1964-02-23"
date = df.parse(simpleDate);
}
return getISODate(date, isoFormat);
}