String to Date with month all lowercase in java - java

I have a string that contains a date, in the following format:
dd-mm-yyyy
with the month that is all lowercased. For example:
25-aug-2019
Now i tried to use SimpleDateFormat to convert my string to a Date, but i have the following exception:
java.text.ParseException: Unparseable date: "25-aug-2019"
at java.text.DateFormat.parse(Unknown Source)
at org.whoislibrary.servers.WhoisCom.parseResponse(WhoisCom.java:37)
at org.whoislibrary.WhoisAbstract.executeQuery(WhoisAbstract.java:44)
at org.whoislibrary.WhoisCommand.executeQuery(WhoisCommand.java:69)
at org.whoislibrary.WhoisMain.main(WhoisMain.java:10)
This is the code that i used:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try {
Date expDate = df.parse(dateString).trim());
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
I think the problem is that MM refers to month name that start with a capital letter (Aug, Jul etc). There is an option, or a class like SimpleDateFormat that help me to convert that string into a Date. or it must be done Manually?

Well in your code you have:
new SimpleDateFormat("dd-MM-yyyy")
Which should be:
new SimpleDateFormat("dd-MMM-yyyy")
Since the month part in your string has 3 letters (MMM) and not 2 (MM)

use dd-MMM-yyyy

Your date "25-aug-2019" is in "dd-MMM-yyyy" format not "dd-MM-yyyy". So you get parse error. You should use "dd-MMM-yyyy" while creating SimpleDateFormat object.

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date expDate = df.parse("25-aug-2019");
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}

Refer javaDocs java.text.SimpleDateFormat

Related

Java Date Exception

want to convert String date into java.util.Date object getting the following exception.
java.text.ParseException: Unparseable date: "04/18/2018 12:00 AM"
input : "04/18/2018 12:00 AM"
private Date convertToDate(String date) {
try {
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy,HH:mm:ss aaa");
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
What I want the date in this format
2018-04-09 09:22:57 AM
For the date format that you specified dd-MMM-yyyy,HH:mm:ss aaa, the date string should be in the following format: 18-Apr-2018,03:22:15 AM. You are not using the correct delimiters in your example.
Tip: this is a nice web resource to test date formats.
That input is not correct format because you try to parse other format... Try to input your date in this way:
18-Apr-2018,03:26:38 AM
For the input you do, the format is: MM/dd/yyyy HH:mm a (04/18/2018 12:00 AM)

How to format a jQuery date using SimpleDateFormat

I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??
System.out.println("Activity IS : IS With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");
System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);
This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help
You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "
Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".
The pattern "yyyy-MM-dd " cannot be right due to two reasons:
a) It starts with a four-digit-year but your input begins with a two-digit-number.
b) It contains a trailing space.
Try to do it.
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdfOverTimeWithDate.parse("07/05/2015");
} catch (ParseException e) {
e.printStackTrace();
}

SimpleDateFormat.parse() - generates wrong date for different date-formats

Below is my code to parse the date using SimpleDateFormat with pattern:
String pattern = "yyyy-MM-dd";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("05-21-2030");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
You can see the date which I passed to parse is different from date format which is specified in SimpleDateFormat. In this case I was expecting kind of excpetion as format is different but it parsed successfully with some different date values. I got the output - Tue Mar 22 00:00:00 IST 12
When I pass the same format like 2030-05-21 it works fine.
Can you guys please let me know how can I prevent such things in my code?
Basically you want SimpleDateFormat to be strict, so set lenient to false.
SimpleDateFormat format = new SimpleDateFormat(pattern);
format.setLenient(false);
If you can afford using Java 8 time API, its formatter works as expected:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate date = LocalDate.parse("2030-05-21", formatter);
System.out.println(date);
LocalDate date2 = LocalDate.parse("05-21-2030", formatter);
System.out.println(date2);
} catch (DateTimeParseException e) {
e.printStackTrace();
}
Output:
2030-05-21
java.time.format.DateTimeParseException: Text '05-21-2030' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.LocalDate.parse(LocalDate.java:400)
at java8.Snippet.main(Snippet.java:25)

date parsing in java

I have written following program to convert date string to sql date object to store in db2 but the ouput shown is 2013-01-02 instead of 2013-02-02. can anyone explain why??
String string = "02/02/2013";
Date date = null;
try {
date = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(string);
java.sql.Date newDate=new java.sql.Date(date.getTime());
System.out.println(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
For month you should use MM instead of mm. mm is used for minutes in hour: -
date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
your format should be
"dd/MM/yyyy"
note that mm is for minutes whereas MM is for Month
Check the Doc

java : java.text.ParseException: Unparseable date

I am reciving a input in this format 2012-01-13T00:00:00.000-05:00 and which i need to convert this into yyyyMMdd Format .
I have also set the SimpleDateFormat.setLenient(false);
This is my coding for parsing the Date
public static String getparsedDate(String date) throws Exception {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
sdf.setLenient(false);
String s1 = date;
String s2 = null;
Date d;
try {
d = sdf.parse(s1);
s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return s2;
}
But i am getting a Exception at
java.text.ParseException: Unparseable date: "201201"
at java.text.DateFormat.parse(Unknown Source)
Could anybody please let me know , what might be the issue ?
You are missing the timezone in your format string. If you check the argument, it is finishing with -05:00 and you are also using Lenient==false.
Unfortunately, the time zone formats available to SimpleDateFormat are not ISO8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC822. Therefore using SimpleDateFormat does not seem as an option in your case (since you use UTC−05:00 as timezone).
Instead of SimpleDateFormat you need to use JodaTime for that type of date format.

Categories

Resources