Unable to parse this date - java

I am getting dates in this format 14-MAY-13 01.17.16.250000 PM and
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss aaa");
simpleDateFormat.parse("14-MAY-13 01.17.16.250000 PM")
Gives me this exception
java.text.ParseException: Unparseable date: "14-MAY-13 01.17.16.250000 PM" ..What should be format of simpledateformat?

Try
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");

The format must be corrected (see the javadoc for more information). You should also set the Locale to the SimpleDateFormat to be sure the month will be parsed with the correct language.
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa", Locale.US);
If you don't give the Locale, SimpleDateFormat will use the default locale of your system which can be not the locale of the given string input.

You need to use the SSSSSS for milliseconds and replace : with . to match this part 01.17.16.250000.
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");

In your Formatstring you expect hh:mm:ss but in your input string your have hh.mm.ss and you also didn't include the miliseconds.
So the format string should be dd-MMM-yy hh.mm.ss.SSSSSS aaa

Simple date format and date formate should be same, try this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss:SSSSSS aaa");
Date d = simpleDateFormat.parse("14-MAY-13 01:17:16:250000 PM");

Related

How to convert date from mm/dd/yyyy to mm dd, yyyy

I want to convert date from 07/02/2019 to July 07, 2019. My input value is 07/02/2019 I want to compare with target value July 07, 2019....Please help me on this...
public static void main(String[] args) throws ParseException {
String sDate1="07/01/2019";
java.util.Date date1=new SimpleDateFormat("MM/dd/yyyy").parse(sDate1);
System.out.println(date1);
Output:Mon Jul 01 00:00:00 IST 2019 which is not my expected value
Try this one.
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
SimpleDateFormat output = new SimpleDateFormat("MMMM dd, yyyy");
Date data = sdf.parse("07/02/2019");
String newDate = output.format(data);
System.out.println(newDate);
Here, you use:
java.util.Date date1=new SimpleDateFormat("MM/dd/yyyy").parse(sDate1);
to parse a date that comes in as String.
Then you print that date without any formatting information.
Thus the answer is quite simple: define a pattern for formatting a Date object as string! Same rules, same patterns. Just not parsing, but formatting for printing!
In other words: you already know the concept, you used a formatter to turn a String into a Date. Now simply turn that around, and provide a pattern to a formatter to turn a Date into a string!
Parse your input date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
LocalDate date = LocalDate.parse(sDate, formatter);
Similarly parse your target date
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu", Locale.ENGLISH);
LocalDate targetDate = LocalDate.parse("July 07, 2019");
Or better yet, define your target date without using a string
LocalDate targetDate = LocalDate.of(2019, Month.JULY, 7);
Compare
if (date.equals(targetDate)) {
System.out.println("Same date");
}
LocalDate also have methods isBefore and isAfter.
This answer is entered from my tablet without trying the code out, so please forgive the typos.

ParseException when parsing 3 character abbreviated month using SimpleDateFormat

Here is my code,
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date dft = (Date) format.parse("16-MAY-2018 09:30:22:000");
I am getting below exception
java.text.ParseException: Unparseable date: "16-MAY-2018 09:30:22"
What's to be used to parse milliseconds?
The pattern should be MMM because there are three characters in the month.
You should also prefer java.time classes to the ones you're currently using if you're on Java 8 or above:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd-MMM-yyyy")
.appendLiteral(' ')
.append(ISO_LOCAL_TIME)
.toFormatter();
LocalDateTime timestamp = LocalDateTime.parse("16-May-2018 09:30:22", formatter);
Use this pattern: dd-MMM-yyyy HH:mm:ss
Date dft = (Date) format.parse("16-05-2018 09:30:22");
OR change it to
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
you are using dd-MM-yyyy HH:mm:ss, so parsable is
16-05-2018 09:30:22
and if you want 16-MAY-2018 09:30:22 then use
dd-MMM-yyyy HH:mm:ss
"16-MAY-2018 09:30:22" is not parsable with that time format. If you want to parse that you have to change date format to "dd-MMM-yyyy HH:mm:ss". The double M is only for numbered months (so should be 05 for May).
Check the SimpleDateFormat javadoc for more details: here

DateFormatException is not thrown on February, April, June [duplicate]

I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd

Parse String Date time in java

I have a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015

Date format in Java

Few questions:
How can I print now (The current Date/Time) in the format below.
How can I convert a string date, into a date object if I know the format, same format as below.
Example:
2011-05-04 19:12:46 -0500
Format:
YYYY-MM-DD HH:MM:SS [+|-]hh[mm]
Try:
Date yourDate = new Date(...);
SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
System.out.print(myDateFormat.format(yourDate);
The link to SimpleDateFormat Javadoc is here.
Check out SimpleDateFormat: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat.format() and SimpleDateFormat.parse()

Categories