Java Unparseable date Exception yyyyMMdd h:m a [duplicate] - java

This question already has answers here:
Why does this SimpleDataFormat parsing fail on Android?
(2 answers)
Closed 6 years ago.
I have this 20160407 4:30 pm data time string and I want to transfer it to timestamp.
Timestamp timestamp = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd h:m a");
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date parsedDate = dateFormat.parse("20160407 4:30 pm");
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (Exception e) {
e.printStackTrace();
}
I got error:
java.text.ParseException: Unparseable date: "20160407 4:30 pm "

I'm currently in US and your code runs fine if I don't specify a locale. However, the same exception raised when I explicitly changed my locale to China as in Chinese we use '下午' for 'pm' and '上午' for 'am', so if you change 'pm' to '下午' in my code, it will work then.
Locale locale = Locale.CHINA;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd h:m a", locale);
Date parsedDate = dateFormat.parse("20160407 4:30 pm");
So please check what your default locale is by Locale locale = Locale.getDefault().
Also, I would suggest you use the newer Java date and time API too.

Related

How to convert Date from (ddmmyyyy HH:mm:ss) to (yyyy-MM-dd HH:mm:ss.SSS)?) [duplicate]

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");

Java SimpleDateFormat: Pattern - ParseException [duplicate]

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);

Simpledateformat ignoring setTimeZone call [duplicate]

This question already has answers here:
Convert date to different timezone [duplicate]
(3 answers)
Closed 5 years ago.
I have the following code:
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(date);
When I call this code with a date object, then it returns the same date (as String obviously) without setting the timezone.
As I understood a java.util.Date object is timezone independent. So if I set the timezone on the SimpleDateFormat then it should change. But it doesn't.
If I check sdf.getTimeZone() then I see that my timezone is set correctly to UTC+03:00.
Does someone know how to solve this problem?
To get the TimeZone in UTC Format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-YYY HH:mm:ss Z",Locale.ENGLISH);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.format(new Date());
the above format gives results as 22-10-2017 18:05:50 UTC.
To get the TimeZone in Local Format (TimeZone of Phone or device)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-YYY HH:mm:ss Z",Locale.ENGLISH);
simpleDateFormat.setTimeZone(TimeZone.getDefault());
return simpleDateFormat.format(new Date());
or
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-YYY HH:mm:ss Z",Locale.ENGLISH);
return simpleDateFormat.format(new Date());
the above format gives results as 22-10-2017 18:05:50 UTC+5:30.(Hint:-For India)
or
the above format gives results as 22-10-2017 18:05:50 IST.(Hint:-For India)
Hint : - if u pass this formatted String to Date like below
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-YYY HH:mm:ss Z",Locale.ENGLISH);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = null;
try {
d =simpleDateFormat.parse("22-10-2017 18:05:50 UTC");
} catch (ParseException e) {
e.printStackTrace();
}
return d.toString();
the returned output will be same as device timezone i.e local Timezone or default timezone . (22-10-2017 18:05:50 UTC+5:30 or 22-10-2017 18:05:50 IST).
even though the above timezone set is UTC you will get output in Device Timezone Only , if You pass your Content Into Your Date Object.
I think, the better way is use Joda Time instead. It's has a ZZ attribute in format, which gives you what you want.
Usage is easy:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ssZZ").withLocale(Locale.RU);

Set timezone to IST, but I still get a wrong-date error [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 5 years ago.
I want to take current time irrespective to the system date. I am using Glassfish server and derby in netbeans. I tried the code below for getting current date according to IST:
DateFormat df = new SimpleDateFormat("yyyymmdd");
df.setTimeZone(TimeZone.getTimeZone("ist"));
String gmtTime = df.format(new java.util.Date().getTime());
java.util.Date parsed = null;
try {
parsed = (java.util.Date) df.parse(gmtTime);
} catch (ParseException ex) {
Logger.getLogger(EmployeePanel.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date date = new java.sql.Date(parsed.getTime());
but I am getting 2017-01-08 instead of 2017-08-08.
m is minute not month (M). The right pattern is:
SimpleDateFormat("yyyyMMdd");
The pattern definition is case sensitive!
For more informations see the javadoc of SimpleDateFormat

How to validate dates in two different formats [duplicate]

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 5 years ago.
I am doing some junit test. I am getting the date as response in the form of date as below :
2017-08-14 00:00:00.0 +0:00
The data which is present in oracle DB is
14-AUG-17 12.00.00.000000000 AM +00:00
I want to use an assert like this but it is failing. can anyone help to make sure that both expected and actual matches.
Assert.assertEquals("14-08-2017", 2017-08-14 00:00:00.0 +0:00);
You can create two Date objects and for the assertion.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
try {
Date parsedDate1 = formatter.parse("14-08-2017");
System.out.println(parsedDate1);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate2 = formatter2.parse("2017-08-1 00:00:00.0 +0:00");
Assert.assertEquals(parsedDate1, parsedDate2);
} catch (ParseException e1) {
}
You can use SimpleDateFormat to make string from date.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Assert.assertEquals("14-08-2017", sdf.format(<your date instance>));

Categories

Resources