ParseException - Unparseable date exception in Java [duplicate] - java

This question already has answers here:
Why can't this SimpleDateFormat parse this date string?
(4 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Closed 2 years ago.
I have oracle database with columns data type "TIMESTAMP(0) WITH TIME ZONE" . Which produces
like 14/11/2019 06:30:00, +03:00 in database .
When I am trying to get this timestamp from java
I got java.text.ParseException: Unparseable date: "28-MAY-12 07.40.03 PM +03:00" . Our project java version is 6 . I tried with
new SimpleDateFormat("dd-MM-yy hh.mm.ss") ,
new SimpleDateFormat("dd-MM-yy hh.mm.ss, Z"),
new SimpleDateFormat("dd-MM-yy hh.mm.ss, z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss, z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss, Z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss")
But none of them worked for me all of throwing java.text.ParseException: Unparseable date error.
In my java side i have date datatype property in my class and i want to retrieve data from oracle database then parse then set to date data type property like below.
serDto.setTarih(new Timestamp(new SimpleDateFormat("dd-MM-yy hh.mm.ss").parse(str).getTime()));
For ex in db timestamp data looks like : 14/11/2019 06:30:00, +03:00 and i want to retrieve as date like above.
Thank you in advance.

A partial solution is changing the pattern so that it parses the name of the month and the am/pm marker. Change MM to MMM and add a:
new SimpleDateFormat("dd-MMM-yy hh.mm.ss a").parse(str)
However this will ignore the time zone information. It will return a date with the expected time (i.e. 07:40:03 PM) in the default local time zone where this code is running, which might be different from the time zone in the input GMT+0300.
There doesn't seem to be a way to parse +03:00 into a time zone with SimpleDateFormat in Java 6, so if you need to parse the time zone this will require more custom code.

Related

Cant display date as yyyy-MM-dd'T'HH:mm:ss:SSS Java [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());

Java iso 8601 universal date time format [duplicate]

This question already has an answer here:
in java I need define date in this format 1999-05-31T13:20:00-05:00 [duplicate]
(1 answer)
Closed 5 years ago.
I want this result on date time: 2008-10-31T15:07:38.6875000-05:00, please help me how i get this result?
I am using following code but unable to get required response.
TimeZone tzone = TimeZone.getTimeZone("UTC");
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
String nowAsISO = dateformat.format(new Date());
You are quite right:
Your format expression is missing seconds :ss, millisecond .SSS (many S for how many digit you want) and the Z timezone tag without '
Using single quote ' in the expression will exclude everything included in them from parsing, ergo you will have it printed like a string.
TimeZone tzone = TimeZone.getTimeZone("UTC");
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ");
String nowAsISO = dateformat.format(new Date());
You can see every possible pattern here

parse String to 'yyyy-mm-ddThh:mm:ss.SSSZ' ISODate java [duplicate]

This question already has answers here:
Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object
(4 answers)
Closed 6 years ago.
How to convert String to ISODate format using SimpleDateFormat, which means that i want to finally get Date in java.util.Date format.
My string will look like 2017-02-17T09:28:03.000Z, i want to convert it to date formt. I can do this in Joda date format, but since i am using mongoDB, it does not accept joda format.
String startDateString1 = "2017-02-17T04:23:17.452Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date startDate = df.parse(startDateString1);
String newDateString = df.format(startDate);
above code is not working.
Your code is not working since Z is a reserved character used to interpret RFC-822 time zones :
RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit
Since Java 7, you can use X to interpret ISO-8601 time zones https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html . The following works :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
However, on my computer,
System.out.println(newDateString);
results in the following output :
2017-02-17T05:23:17.452+01
Alternatively, if you are sure to have only UTC dates, you could escape the Z letter with simple quotes :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
And here is the displayed result :
2017-02-17T04:23:17.452Z
You can do it in Java 8 like below.
Instant instant = Instant.parse("2017-02-17T09:28:03.000Z");
Date date = Date.from(instant);
You could use javax.xml.bind.DatatypeConverter.parseDateTime("2017-02-17T04:23:17.452Z") which will return a Calendar object. You can call getTime() on it to get a Date object.

Change Time zone and date format [duplicate]

This question already has answers here:
How to set the TimeZone for String parsing in Android
(3 answers)
Closed 6 years ago.
I get from the server is like 2017-01-24T16:16:30.690Z.
This date is in GMT time zone.
I want to convert this time into GMT+6 time zone as well as time format.
My expected result is: 24 January 2017 22:16
See above comments. If you apply those, try:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2017-01-24T16:16:30.690Z", new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+06:00"));
sdf.applyPattern("d MMMM yyyy HH:mm");
String formatted = sdf.format(date);
Worked for me.

Java Timezone Conversion : Help Required [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Timezone conversion
I have to convert given time and timezone with some other timezone using Java code.
For Example :
I have to convert 28 Sept 2:00 PM IST in Canada timezone with considering DST (Day Light Saving Time) as well.
Can someone help me?
Try getTimeZone() and setTimeZone() along with Calendar class
TimeZone oztz = TimeZone.getTimeZone("Canada/Atlantic");
Calendar datetime = Calendar.getInstance( oztz );
See this link for all Time-Zones :
http://snipplr.com/view/23131/timezone-enum/
Probably you are looking for something like this :
Converting Times Between Time Zones
The Calendar class has built in methods for doing the conversion.
You can set the desired timezone with setTimeZone.
Also see the getTimeZoneOffset from the Date class.
You could use JodaTime and its support for different timezones.
Namely, if you know you have string ISO format or a specific known format (like it looks like), you could do something like:
DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZone("Asia/Kolkata");
DateTime newDate = fmt.parseDateTime([your input date]);
// You can manipulate your date here...
String newString = fmt.withZone("Canada/Atlantic").print();
This handles internally all Timezone matters, included DST.
Simple way is set Datetime and Timezone to Calender and getTime. Below is sample code which will set Timezone and user specific time.
Date date =new Date(2012,9,28,2,00,00); //Set time to Date
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Canada/Atlantic"));
calendar.setTime(date);
System.out.println("Timezone :: " + calendar.getTimeZone());
System.out.println("Time :: " + calendar.getTime());
Output:
Timezone :: sun.util.calendar.ZoneInfo[id="Canada/Atlantic",offset=-14400000,dstSavings=3600000,useDaylight=true,transitions=228,lastRule=java.util.SimpleTimeZone[id=Canada/Atlantic,offset=-14400000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Time :: Mon Oct 28 02:00:00 GMT 3912
You will get all information from TimeZone useDaylight(true/false), dstSaving(1 hour for canada)
For more functionality of TimeZone please refer below link:
http://biese.wordpress.com/2006/10/23/java-daylight-saving-time-and-time-zone/
Here is link to get DayLightSavingTime information:
http://timeanddate.com/worldclock/clockchange.html?n=1187&year=2012

Categories