Trouble Parsing Date with SimpleDateFormat due to "T" Character - java

2015-11-03T17:33:27
I currently get the above date as a String from a JSON call. The problem is, I have no idea how to parse the T in between the 3 and the 17. When I use SimpleDateFormat, shown below, I keep getting unparseable date. What exactly do I do with the T here?
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
messageDate = format.parse(recentMessage.getJSONObject("message_data").getString("sent_at"));

This should work
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date messageDate = format.parse("2015-11-03T17:33:27".replace("T", " "));
Also keep Timezone in mind when parsing the dates.

Related

Unparseable date when parsing with AM/PM

I'm having a problem when parsing a date from a string.
This is my code:
String date = "04/01/2016 03:52:33 PM";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
Date dt = format.parse(date);
But it keeps throwing an exception:
java.text.ParseException: Unparseable date: "04/01/2016 03:52:33 PM" (at offset 20)
Any help would be appreciated.
The am/pm marker from your default Locale (Peru - Spanish) probably doesnt match that of the input String
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
You don't need two aas.
"dd/MM/yyyy hh:mm:ss a"

How to convert date strings from different TimeZones to Date object in one TimeZone

Im working on an RSS reader software. I get items with their pubDate (publish date) values as string, convert them to Date object, and put them to my DB. However, when I check my DB, I saw some interesting values such as the date of tomorrow.
I research this situation and found that it is about time zone value Z. For example when I get "Mon, 26 May 2014 21:24:29 -0500", it becomes "2014-05-27 05:24:29", the next day !
All I want is to get dates in any timezone and convert them to date in common timezone, such as my country's.
Here is my code :
public static String convert(String datestr) throws ParseException {
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return resultFormatter.format(date);
}
And I use the method like that :
System.out.println(convert("Mon, 26 May 2014 21:24:29 -0500"));
The output is : 2014-05-27 05:24:29
Any idea ?
Since you haven't set a time zone, it's using your system's default.
Set a specific IANA time zone.
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
resultFormatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return resultFormatter.format(date);
Looks like you passed a Date with timezone, but given a wrong format. If you are passing timezone like "-0500" you should rather use:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Remember that the system will always display the date using the current, default timezone (TimeZone.getDefault()) unless you override it by:
resultFormatter.setTimeZone(...)
This is working as expected. The date is converted as per your system's timezone.
Check the UTC offset of your system and replace it in the sample date string and look at the output.
For e.g: India is UTC+5:30
String datestr="Mon, 26 May 2014 21:24:29 +0530";
output:
2014-05-26 21:24:29
Alternate solution
If you don't want to consider the timezone of the input date string then simply truncate this information and remove zzz from pattern as well as shown in below code:
String datestr = "Mon, 26 May 2014 21:24:29 -0530";
datestr = datestr.replaceAll("\\s[-+](\\d+)$", ""); // truncate the timezone info if not needed
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); // remove zzz from the pattern
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(resultFormatter.format(date));

Unparsable date exception

I'm currently working on some simple project in Java and I have date in the following string:
String dateString = "Sun 7/14 03:44 AM 2013";
and want to to convert this string to Date object. I'm using following lines of code to do that. I searched site and found solution how to do this with DateFormatter:
DateFormat format = new SimpleDateFormat("EEE M/dd hh:mm a yyyy");
Date d = format.parse(dateString);
But I'm probably doing something wrong, because I always get exception:
Unparseable date: "Sun 7/14 03:44 AM 2013"
This seems to be problem with pattern I'm using but tried different patterns and nothing work.
Certain fields such as the day of week fields and/or AM/PM marker may not match those from your default Locale. ParseException has the method getErrorOffset to determine exactly where the pattern does not match.
try
DateFormat format =
new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
It is important to add Locale as you are parsing language day of week names.
String dateString = "Sun 7/14 03:44 AM 2013";
DateFormat format = new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.US);
Date d = format.parse(dateString);
I tried this out and the following worked,
String stringDate = "Sun 7/14 03:44 AM 2013";
DateFormat format = new SimpleDateFormat("EEE MM/dd hh:mm a yyyy");
System.out.println("Parsed Date = "+format.parse(stringDate));
The output was as follows
Parsed Date = Sun Jul 14 03:44:00 BST 2013
SimpleDateFormat formatter = new SimpleDateFormat("/* type your own format*/");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
try this code
The modern answer for the sake of completeness. While the other answers were good answers in 2013, Date, DateFormat and SimpleDateFormat are now long outdated, and I recommend you replace them with their modern counterparts:
DateTimeFormatter parser
= DateTimeFormatter.ofPattern("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateString, parser);
The result is a LocalDateTime of 2013-07-14T03:44 as expected.
The format pattern string is still the same, and the need for an English language locale is the same.

small issue while coverting string value in Date

I am unable to covert String values in The Java.util Date format.
This is my code
String string = "24-Nov-2012 14:21:56";
Date soldate = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz").parse(string); // problem in this line
System.out.println(soldate);
Please help me to solve this issue.
new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz")
You don't have that zzz - TimeZone part in your string. So, your format cannot parse your string. As it is different from the actual string format.
Just remove it: -
new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz") where zzz is the place holder for Timezone part for your date string.
So remove it if your date string is not going to have any timezone information and use this :
SimpleDateFormat("dd-MMM-yyyy HH:mm:ss")
See the link below for other options that yu can use with SimpleDateFormat
SimpleDateFormat javadoc

Parsing a java Date back from toString() [duplicate]

This question already has answers here:
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I have a String containing the result of toString() called on an instance of java.util.Date. How can I parse this value back to a Date object?
The Java docs say that toString() converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
but of course there is no such format tag as "dow" or "mon".
Could someone please help me with this problem. Please note that unfortunately the toString() call is in a piece of code out of my control.
If you don't have control over the code that's generating the String:
To parse the toString() format you need to set the SimpleDateFormat locale to english and use the format: "EEE MMM dd HH:mm:ss Z yyyy".
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));`
I didn't test but something like this probably would work:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM HH:mm:ss z yyyy");
Date date = sdf.parse(dateStr);
If not, try to correct it using documentation:
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString()
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
First take a look at all the date formats provided by java Date Formats.
And you can use SimpleDateFormat class to do what you want.
public class DateFormatTest
{
public DateFormatTest()
{
String dateString = // in "dow mon dd hh:mm:ss zzz yyyy" format
SimpleDateFormat dateFormat = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
}
public static void main(String[] argv)
{
new DateFormatTest();
}
}
}
Use simpledateformat. Find the doumentation here:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Suppsoe you get String of "dateString" ;
SimpleDateFormat sdf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date date = sdf.parse("dateString");

Categories

Resources