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);
Related
This question already has answers here:
Change date string format in android
(8 answers)
Date format conversion Android
(9 answers)
Closed 2 years ago.
I get datetime data from soap web service to get string:2020-05-03T00:00:00.
Is there a way to split the string into dd / mm / yyyy, and if it is null then omitted?
I am using a substring(0,10) , but it doesn't work very well.
You can do it this way.
Parse the input string.
LocalDateTime dateTime = LocalDateTime.parse("2020-05-03T00:00:00");
Generate text representing the value of that LocalDateTime object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
03/05/2020
For early Android before 26, see the ThreeTenABP & ThreeTen-Backport projects, a back-port of most of the java.time functionality.
try with this
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
date = inputFormat.parse("2020-05-03T00:00:00");
String formattedDate = outputFormat.format(date);
System.out.println("coverted: "+formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: 03-05-2020
You can use java Calendar class to get Date, Month and Year as shown in below
String s = "2020-05-03T00:00:00";
Calendar calendar = new GregorianCalendar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj = null;
try {
dateObj = format.parse(s);
calendar.setTime(dateObj);
int date = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;// As start from 0
int year = calendar.get(Calendar.YEAR);
System.out.println("Formatted Date -> "+ date + "/" + month + "/" +year);
} catch (ParseException e) {
e.printStackTrace();
}
You can do many more thing after converting it to Calendar class object, like hour, minutes, day of week etc
This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 3 years ago.
I am trying to convert date from (ddmmyyyy HH:mm:ss) to (yyyy-MM-dd HH:mm:ss.SSS) format.
Below is the code :
String startDate="06162019 00:00:00";
SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss");
SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try{
String outDate = "";
Date date = inSDF.parse(startDate);
System.out.println(date);
outDate = outSDF.format(date);
System.out.println(outDate);
}catch (final Exception e) {
e.getMessage();
}
But i am getting wrong result :
Sun Jan 06 00:00:00 GMT 2019
2019-01-06 00:00:00.000
Any help would be appreciated?
I assume that you work with Java 8 or later. If so please drop old and horrible java.util.Date and and even worse java.text.SimpleDateFormat they are dead and buried. Switch to use of java.time package. in order to solve your problem you would need to do this:
String startDate="06162019 00:00:00";
DateTimeFormatter inSDF = DateTimeFormatter.ofPattern("MMddyyyy HH:mm:ss");
DateTimeFormatter outSDF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
try{
System.out.println(outSDF.format(inSDF.parse(startDate)));
}catch (Exception e) {
e.getMessage();
}
See this code run live at IdeOne.com.
2019-06-16 00:00:00.000
Read about DateTimeFormatter. Also you might find this article interesting: Java 8 java.time package: parsing any string to date
A lower-case m represent minutes, while an upper-case m represents months.
You need to change SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss"); to SimpleDateFormat inSDF = new SimpleDateFormat("ddMMyyyy HH:mm:ss");
This question already has answers here:
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Closed 5 years ago.
I am trying to convert string of format yyyy-mm-dd to dd-MMM-yy. I am getting correct year and days but for month it is showing only jan irrespective of my input. How to fix it?
String input = "2013-09-14";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
Date date = null;
try {
date = format1.parse(input);
String temp = format2.format(date);
System.out.println(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
14-Jan-13
But I should get:
14-Oct-13
mm is for minute
you need MM or MMM for month.
See SimpleDateFormat for reference.
mm is for minutes. MM is for months.
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
I would suggest latest API LocalDate which not requires try/catch and which is easier to use :
String input = "2013-09-14";
LocalDate inputDate = LocalDate.parse(input, DateTimeFormatter.ISO_DATE);
String format = inputDate.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
System.out.println(format);
DateTimeFormatter.ISO_DATE is shortcut for DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate inputDate = LocalDate.parse(input); would also work because ISO_DATE the default format
DateTimeFormatter doc (patterns)
The pattern is case sensitive:
MM is month, mm is Minute:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
For more Information read The javadoc of SimpleDateFormat
personally i used a pattern to do this,
LocalTime w = LocalTime.MIDNIGHT.plus(d);
s = DateTimeFormatter.ofPattern("HH:mm:ss").format(w);
and i advise you to use java 8 date type instead
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:
Convert a string to a GregorianCalendar
(2 answers)
Closed 6 years ago.
This is the code to get date format
GregorianCalendar calDate = new GregorianCalendar();
The constructor will hold value of format as 2015,7,15
what I want to achieve is to be able to set this format from a text field. For example
GregorianCalendar calDate = new GregorianCalendar(Formats.getText());
But I have errors and is not working.
Please whats is right code?
Resolved with the below code
String nnhh= ""+Firstname.getText();
String someDate = ""+nnhh;
SimpleDateFormat sdf = new SimpleDateFormat("MM,dd,yyyy");
try {
Date date = sdf.parse(someDate);
long dd=date.getTime();
Firstname.setText(""+dd);} catch (ParseException e) {
e.printStackTrace();
}
and dd was in TimeInMillis just as I wanted. Thanks
To be able to parse it, I would suggest you to use something like:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("y M d VV m s H");
The formatter requires all of these elements. (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html for formatting details).
And this should be all you need:
GregorianCalendar calDate = GregorianCalendar.from(ZonedDateTime.parse(dateToParse, formatter));