Parsing timestamp with timezone in java? - java

I'm trying to parse a string of format timestamp with timezone obtained from a DB. The String is as follows :
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSZ");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date d1 = mdyFormat.parse("2014-04-01 15:19:49.31146+05:30");
String mdx = sdf.format(d1);
System.out.println(mdx);
Problem is, I get an error saying :
Exception in thread "main" java.text.ParseException: Unparseable date: "2014-04-01 15:19:49.31146+05:30"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.karthik.Timestampvalidate.main(Timestampvalidate.java:31)
Does anyone know how to fix this ?

You need to use X instead of Z:
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSX");
See the javadoc for more info.
Note: only available in Java 7+.

If you get to use the new JSR 310 date/time APIs in Java 8, you can use the XXX format to parse the timezone. You need three Xs to get the specific colon-separated offset that you're using.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSXXX");
TemporalAccessor dateTime = formatter.parse("2014-04-01 15:19:49.31146+05:30");
// returns: {OffsetSeconds=19800},ISO resolved to 2014-04-01T15:19:49.311460

Related

java.text.ParseException: Unparseable date “yyyy-MM-dd'T'HH:mm:ss.SSSXXX” - SimpleDateFormat

I used the follow code to parse datetime:
String parseDate(String tranDateTime, String originalDateFormat, String toDateFormat) throws ParseException {
SimpleDateFormat originalMonthYear = new SimpleDateFormat(originalDateFormat);
Date date = originalMonthYear.parse(tranDateTime);
SimpleDateFormat formatter = new SimpleDateFormat(toDateFormat);
return formatter.format(date);
}
And tried to parse a datetime value as below :
parseDate("2017-08-16T00:00:00Z",
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "yyyy-MM-dd HH:mm:ss");
An exception is thrown :
java.text.ParseException: Unparseable date: "2017-08-16T00:00:00Z"
However, when I change date value to "2017-08-16T02:54:15.537Z", the function worked fine. I don't know why.
The problem is coming from :
Date date = originalMonthYear.parse(tranDateTime);
This date
2017-08-16T00:00:00Z
Doesn't match the pattern
yyyy-MM-dd'T'HH:mm:ss.SSSXXX
Because milliseconds are missing (SSS part)
So
Date date = originalMonthYear.parse(tranDateTime);
Throws the exception
java.text.ParseException: Unparseable date: "2017-08-16T00:00:00Z"
Solutions
Change your date 2017-08-16T00:00:00Z into 2017-08-16T00:00:00.000Z (add milliseconds to your date)
Change your pattern yyyy-MM-dd'T'HH:mm:ss.SSSXXX into yyyy-MM-dd'T'HH:mm:ssXXX (remove milliseconds from your pattern)
You asked in a comment:
Moreover, the datetime value could be "2017-08-16T00:00:00Z" or
"2017-08-16T00:00:15.537Z" retrieved from database. Do we have a
general pattern to parse both formats?
Even better, they can both be parsed without an explicit pattern:
System.out.println(OffsetDateTime.parse("2017-08-16T00:00:00Z"));
System.out.println(OffsetDateTime.parse("2017-08-16T00:00:15.537Z"));
This prints:
2017-08-16T00:00Z
2017-08-16T00:00:15.537Z
To format, for example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(OffsetDateTime.parse("2017-08-16T00:00:00Z").format(formatter));
This prints
2017-08-16 00:00:00
I am using and warmly recommending java.time, the modern Java date and time API. SimpleDateFormat and Date are long outmoded and the former in particular notoriously troublesome. The modern API is so much nicer to work with.
Your strings (in spite of differences) both conform to ISO 8601, the standard for date and time formats. The java.time classes parse these formats without any explicit formatter.
Link: Oracle tutorial: Date Time explaining how to use java.time
Change 2017-08-16T00:00:00Z to 2017-08-16T00:00:00.000Z, it works for me.
Problem source :
The problem come from trying to pars date value 2017-08-16T00:00:00Z with a date format yyyy-MM-dd'T'HH:mm:ss.SSSXXX, in the line :
Date date = originalMonthYear.parse(tranDateTime);
See the difference between the value and format:
______________________________
|2017|08|16| T |00|00|00Z |
|yyyy|MM|dd|'T'|HH|mm|ss.SSSXXX|
|____|__|__|___|__|__|_________|
Solution :
Simple one is :
Date date = originalMonthYear.parse("2017-08-16T00:00:00.000Z")
But, if you will get many differents formats, you can use something like (never tested):
List<SimpleDateFormat> myUsedPatterns = new ArrayList<>();
myUsedPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));
myUsedPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
for (SimpleDateFormat myPattern : myUsedPatterns) {
try {
SimpleDateFormat originalMonthYear = new SimpleDateFormat(myPattern );
Date date = originalMonthYear.parse(tranDateTime);
SimpleDateFormat formatter = new SimpleDateFormat(toDateFormat);
return formatter.format(date);
} catch (ParseException e) {
// Loop
}
}

Java parse date using SimpleDateFormat

I am trying to convert a English date from String format to Date and if possible get it as French format.
For example I get something in this format : "2014-07-21T14:31:08+0200"
I am using this line but this raise a ParseException any idea ?
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.Z");
You have a . in front of the Z inside the format.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = simpleDateFormat.parse("2014-07-21T14:31:08+0200");
happily parses.

convert string into date format in java

I want to convert this string to the following date format.
String s = "2-26-2013";
Date date = new SimpleDateFormat("EEEE, MMMM/dd/yyyy").parse(s);
System.out.println(date);
I'm getting this error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2-26-2013"
at java.text.DateFormat.parse(DateFormat.java:357)
Well yes. The argument you pass into the constructor of SimpleDateFormat says the format you expect the date to be in.
"EEEE, MMMM/dd/yyyy" would be valid for input like "Tuesday, February/26/2013". It's not even slightly valid for "2-26-2013". You do understand that you're parsing the text at the moment, not formatting it?
It looks like you want a format string of "M-dd-yyyy" or possibly "M-d-yyyy".
If you're trying to convert from one format to another, you need to first specify the format to parse, and then specify the format to format with:
SimpleDateFormat parser = new SimpleDateFormat("M-dd-yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM/dd/yyyy");
Date date = parser.parse(input);
String output = formatter.format(date);
Date date = new SimpleDateFormat("MM-dd-yyyy").parse(s);
The argument to SimpleDateFormat defines the format your date it in. The above line matches your format, and works. Your example does not match.
Instead of using MMMM/dd/yyyy you need to used MM-dd-yyyy. SimpleDateFormat expects the pattern to match what its trying to parse.

convert String "30-MAR-07" to date object

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
dt = formatter.parse(temp[0]);
gives me the error
java.text.ParseException: Unparseable date: "30-MAR-07"
Is there any way that I can format the given String to a Date object without having to write my own string splitting and translating-to-months-in-numerals methods? Thanks
Use correct format, as follows,
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Your SimpleDateFormat pattern is completely incorrect for "30-MAR-07". It's got the values in the wrong order, it uses the wrong separator, and the wrong month format. You want:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
You may also want to specify the locale, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);
I've checked that this works - at least on my machine :)
Try this... (new SimpleDateFormat("dd-MMM-yy")
System.out.println(new SimpleDateFormat("dd-MMM-yy").format(new Date("30-MAR-07")));

How to convert String object to Date object? [duplicate]

This question already has answers here:
How to convert ISO 8601 date (string) to Date?
(3 answers)
Closed 5 years ago.
How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
You'll have to use a DateFormat. It can be as simple as:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
See Sun's Java tutorial and the class SimpleDateFormat
Use a SimpleDateFormat with a format string, which matches your actual format:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
you should parse the string with the SimpleDateFormat class
use
Date date = DateFormat.getInstance.parse( dateString );
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
format= "dd/MM/yyyy"

Categories