JAVA - Format a custom string using SimpleDateFormat [duplicate] - java

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Java string to date conversion
(17 answers)
Closed 5 years ago.
I'm having trouble formatting a custom String back to a Date object. What i have:
String customString = "October 14, 2015;
Date date = new Date();
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy");
try {
date = s.parse(customString);
} catch (ParseException e) {
e.printStackTrace();
}
I always get a unappeasable date exception. Any pointers of what i'm doing wrong is appreciated.

Your pattern must be: new SimpleDateFormat("MMM dd,yyyy");
For more informations about SimpleDateFormat see the javadoc

Read the docs, https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
The argument of the constructor should have the format of the date you want to input.
For instance, if you want the full month name should have "MMMMM".
Just make the following change and the program will work.
SimpleDateFormat s = new SimpleDateFormat("MMMMM dd, yyyy");

Related

How to convert "2019-04-24" to "24-Apr-2019" date format? [duplicate]

This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Closed 1 year ago.
I am trying to convert "2019-04-24" to "24-Apr-2019" this but it is converting it as "24-Jan-19".
This is the code I am using:
public static String dateFormatConvert(String date, String srcDateFormat, String targetDataFormat) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(srcDateFormat);
Date srcDate = dateFormat.parse(date);
DateFormat destDateFormat = new SimpleDateFormat(targetDataFormat);
return destDateFormat.format(srcDate);
}
Calling it:
String date1 = CommonUtil.dateFormatConvert("2019-04-24", "yyyy-MM-DD", "DD-MMM-YY"); -> This is one of the format used (DD-MMM-YY) out of many
System.out.println(date1);
What's going wrong here?
According to the Documentation.
D - Day in year
d - Day in month
The correct code would be:
date1 = dateFormatConvert("2019-04-24", "yyyy-MM-dd", "dd-MMM-yyyy");

Exception trying to convert String to UTC date format [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 5 years ago.
I tried to convert the string date to UTC date with below Java code snippet but getting Unparseable date format exception. Please find the code below and help me fix this issue.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm-SSSZ");
String strDate= "2017-06-01T01:30-0400";
try {
Date date = formatter.parse(strDate);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
date = new Date(formatter.format(date));
System.out.println(date+"gmt");
} catch (ParseException e) {
e.printStackTrace();
}
Thanks in advance.
Cannot parse the
"yyyy-MM-dd'T'HH:mm-SSSZ"
Becuase of did not match with your
strDate= "2017-06-01T01:30-0400"
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm-SSS'Z'");
String strDate= "2017-06-01T01:30-040Z";

Best way to transform date string into an date object [duplicate]

This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Java string to date conversion
(17 answers)
Closed 6 years ago.
What is the best way to transform like October-2016 into an Date Object?
When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
it throws an exception, because it cannot convert this type of date.
since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat
Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
String st = "October-2016";
DateFormat format = new SimpleDateFormat("MMMM-yyyy");
Date date = format.parse(st);
System.out.println(date);
Both working same:
Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
System.out.println(date);
System.out.println(date1);

SimpleDateFormat and Date object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Month name as a string
(12 answers)
Closed 6 years ago.
In my android app I retrieve from an api the current day as.
long dateTime = innerJSON.getLong("dt");
Date weekDay = new Date(dateTime * 1000L);
SimpleDateFormat outFormat = new
SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
And I get the current day ie Monday.
For the date I use the same Date object but with a different SimpleDateFormat object.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd-M-yyyy");
String date = outFormat1.format(weekDay);
And that gives me,
28-5-2016
which is fine. However, I want the date to have a format of
28 May
Any ideas on that? I checked the official orarcle's site,but I can't find a solution.
Thanks,
Theo.
Try this
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
you will get the output you required.
You need to use dd MMM format here.
More details provided here in official documentation.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
String date = outFormat1.format(weekDay);
haha. That was easy.
SimpleDateFormat("dd MMM");
It's summer now and sunny,so I can't concetrate:):)

Convert String to Date object [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
What is the preferred way to convert a string to a date in Java?
20151108235959 --> (15-Nov-08 11:59:59 PM)
This should work as you specified in your question.
DateFormat dateFormat = new SimpleDateFormat("yy-MMM-dd HH:mm:ss a");
System.out.println(dateFormat.format(new Date()));
The date format you have in your question is something like:
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHmmss");

Categories