This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 2 years ago.
I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:
String date = "2020-09-05";
Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:
Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);
For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".
Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?
You need to make a distinction between parsing the date (turning a String into a Date or LocalDate class) and formatting a date (turning the Date or LocalDate class instance to a String).
To parse the initial string, use:
LocalDate convertedDate = LocalDate.parse(date)
To format the convertedDate, use:
String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"))
EDIT: I am assuming you are at least on Java 8, so you can use the newer date/time classes. See https://www.baeldung.com/java-8-date-time-intro for some more info.
You are mixing up parsing and formatting: your code is trying to parse a string given the format. You’re telling Java you’re expecting the date to contain the week name.
You need to first parse your date in the format it’s in. This will give you a date. You next need format your date with the desired format, which will give you a string:
final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);
ok here's the code:
String sDate1="1998-12-31";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sDate1);
System.out.println("EEE, d MMM yyyy formatted date : " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));
Related
This question already has answers here:
Format a date using the new date time API
(5 answers)
Closed 2 years ago.
I have following date in String.
"2022-02-01T20:32:00.000Z"
And i want to format this date to following pattern
MM/dd/yyyy HH:mm:ss a
I tried following solution from another StackOverflow question.
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate date = LocalDate.parse("2022-02-01T20:32:00.000Z", inputFormatter);
String formattedDate = outputFormatter.format(date);
System.out.println(formattedDate); //02/01/2022
but this format gives only date and i want to time with date. so i added time format in pattern like this.
1.
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a");
2.
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a").withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault());;
but it gives me following exception.
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
So how can i achieve "MM/dd/yyyy HH:mm:ss a" pattern from "2022-02-01T20:32:00.000Z" string date?
You can make it with two lines as this :
OffsetDateTime offset = OffsetDateTime.parse("2022-02-01T20:32:00.000Z");
String output = offset.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));
Output
02/01/2022 08:32:00 PM
Your string "2022-02-01T20:32:00.000Z" is formatted as the default format of OffsetDateTime, so all you need to do is to convert it to OffsetDateTime and then use another formatter to get the desired output.
As you are using a in the end of your pattern, I would suggest to use hh instead of HH.
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I want to get the Date object which contains Date in the form yyyy-mm-dd after adding few months to existing Date object.
Using DateFormat object I have tried this way but its not giving the output as I want. How can I get this Date format in Java?
My code-
Calendar c=Calendar.getInstance();
DateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
Date d=cal.getTime();
c.add(Calendar.MONTH,10);
Date newd1=c.getTime();
String news=sdf.format(newd1);
Date dnew=(Date)sdf.parse(news);
String news has the date of format yyyy-mm-dd but when I use parse on that String it results Date object which is of format "Thu Jan 21 00:14:00 IST 2016". How can I get the Date object in the form of "yyyy-mm-dd" using String news in the above code.
java.util.Date doesn't have format. Its just Date.
You can make it print its values in any form you want, by using SimpleDateFormat
You can try as this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String news = sdf.format(new Date());
//for testing
System.out.println(news);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-
Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);
dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.
I want something similar which will return formatted date in Date data type.
That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.
Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.
Given your example code, it would look like this:
String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);
This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 1 year ago.
I have a date and time picker and I build a string date using these two. Here's a sample date and time string.
"11/6/2013 09:23"
Now I need to convert them into a date and convert them to this format "yyyy-MM-dd'T'HH:mm:ss.SSS".
My problem is I'm having this error in my logcat:
11-06 21:23:53.060: E/Error(26255): Unparseable date: "11/6/2013 09:23" (at offset 2)
I'm using this code to do the conversion of string to date, and the date to a formatted string.
Date d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = d.toString();
When I use new Date and get the current date, it works fine. I guess the format of my string is wrong. But I'm displaying it on an edittext in that format. I want to stay it in that way. Is there anyway to convert this string format to a date? Any ideas guys? Thanks!
I think you are trying to do 2 things at once. In order to convert any String to another String in a different format you need to:
Parse the string with a DateFormat containing the current format, this returns a Date; then
take your newly obtained Date and format it under the desired DateFormat
SimpleDateFormat formatOne = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = formatOne.parse(etaToDeliverArrivedAtShipper.getText().toString());
SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
String result = formatTwo.format(date)
you are trying to parse in yyyy-MM-dd'T'HH:mm:ss.SSS but it is not. it is probably yyyy/M/dd HH:mm:ss
What you probably want is:
Date d = new SimpleDateFormat("yyyy/M/dd HH:mm:ss", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).format(d);
This is simply because your pattern is not matching the date string you are trying to parse. There are 3 errors in your pattern :
You use MM which means the month should be on two digits, while in the date it is only 1 digit
You use .SSS which means there are milliseconds but your date is not that precise
You are using the wrong delimiter
So the right pattern should be : yyyy/M/dd HH:mm:ss
To get the desired format, then create a new SimpleDateFormat object with the desired pattern and use the parse(Date) method giving it the Date object previously returned by parse().