I'm having huge difficulties with simple date format. First of all, I know not all tutorials on all sites are actually good, but the fact that all non trivial formats (not dd/MM/yyyy) gave a parse exception (plus my own tests don't work as expected) is rather frustrating to me.
This is the site in question: http://www.mkyong.com/java/how-to-convert-string-to-date-java/
And I don't understand why something as simple as:
private static void unexpectedFailure() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Throws a parse exception.
Also besides that, I'm trying to parse my own dates. This code gives strange results (unexpected I would say):
public static void doSomething(List<String> list) {
Iterator<String> iter = list.iterator();
String[] line = iter.next().split(" ");
System.out.println(Arrays.toString(line));
DateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
format.setLenient(true);
try {
System.out.println(line[0]+" "+line[1]);
System.out.println(format.parse(line[0]+" "+line[1]));
} catch (ParseException e) {
System.out.println("In theory this should not get caught.");
}
}
Prints out this:
[06/08/2015, 13:51:29:849, DEBUG, etc...]
06/08/2015 13:51:29:849
Thu Aug 06 13:51:29 EEST 2015
Thu Aug 06 13:51:29 EEST 2015 WHAT? WHY?
EDIT I'll try and explain. In my last code snippet I'm just trying to determine if the string is a date, and it passes "the test". However when I'm printing it out the format is simply bizzare. I'm starting to think that is because I'm printing a date. How can I even print a DateFormat? What I was expecting was dd/MM/yyyy hh:mm:ss not ddd MMM 06? hh:mm:ss G YYYY
And I don't understand why something as simple as:
(code snipped)
Throws a parse exception.
My guess is that it's tripping up over Jun which may not be a valid month abbreviation in your system default locale. I suggest you specify the locale in your SimpleDateFormat constructor:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Then you're dealing with a locale which definitely has Jun as a month abbreviation.
That said, where possible I'd suggest using numeric formats where possibly, ideally ones following ISO-8601, e.g.
yyyy-MM-dd'T'HH:mm:ss
However when I'm printing it out the format is simply bizzare.
No it's not. You're effectively using
Date date = format.parse(line[0]+" "+line[1]);
System.out.println(date);
So that's calling Date.toString(), documented as:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
So that's working as expected. However, you want to format the date using your SimpleDateFormat - so you need to call format:
System.out.println(format.format(date));
Of course that just checks that it can round-trip, basically.
As a side note, I suspect you want HH (24-hour clock) instead of hh (12-hour clock) in your format string.
Related
I have looked at many examples and can still not find an answer to match my problem. I have now been stuck for an hour and need help.
I have many strings that are formated like this:
Wed, 06 Nov 2013 18:14:02
I have created a function to convert these strings into Date:
private static Date toDate(String pubDateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
pubDateString = pubDateString.substring(0, 25);
Date date = null;
try {
date = dateFormat.parse(pubDateString);
} catch (ParseException e1) {
e1.printStackTrace();
}
return date;
}
What I get is a ParseException:
java.text.ParseException: Unparseable date: "Wed, 06 Nov 2013 18:14:02"
Could anyone help me on my first challenge? Thanks.
edit : I tried HH, and kk
(When I originally answered the question, the format string used hh - it has since been changed.)
You're using hh for the hours part, which is a 12-hour format - but providing a string which uses "18". You want HH.
Additionally, I'd suggest explicitly specifying the locale if you know that the values will always use English names.
I've verified that if you specify the locale explicitly, the code definitely works - at least under Oracle's Java 7 implementation:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss",
Locale.US);
If it wasn't working for you without the locale being specified (but with HH) that's probably why - presumably your system locale isn't English, so it was expecting different month and day names.
I need to convert a string to a Date object using SimpleDateFormat.
I use "MMM. dd yyyy" to parse strings like "Dec. 30 2011", with 3 month char and a dot.
It fails for "May 30 2011" so I have to catch the exception and re-try with "MMM dd yyyy". It works for "May", so far so ugly but works.
But when it get to "Sept. 11 2011", all of the above plus "MMMM. dd yyyy" and "MMMM dd yyyy" fail. So I can't parse it at all.
What is going on?
try this pattern -
MMM'.' dd yyyy
Code -
DateFormat df = new SimpleDateFormat("MMM'.' dd yyyy");
Date date = df.parse("Dec. 30 2011");
Hope it will help you to make fun :)
If it were me, instead of trying different patterns and rely on exception handling, I would just substring the first 3 letters, append the last 7 letters, and parse it with MMMdd yyyy.
I think there is no out-of-the-box solution for tolerant date parsing. Neither JDK nor the often used Joda Time library support this directly. But it should be relatively easy to write a helper method that tries different date formats until it succeeds (or fails).
private static final DateFormat[] FORMATS = new DateFormat[] {
new SimpleDateFormat("MM. dd yy"),
new SimpleDateFormat("MMM. dd yy"),
new SimpleDateFormat("MMM dd yy") };
public static synchronized Date parse(String dateString) throws ParseException {
ParseException e = null;
for(DateFormat format : FORMATS) try {
return format.parse(dateString);
} catch (ParseException ex) {
if (e == null || e.getErrorOffset() < ex.getErrorOffset()) e = ex;
}
throw e;
}
Java can only parse 3-letter abbreviations (as MMM) or full month names (as MMMM).
You must massage your input into a 3-letter abbreviation. The simplest way is using regex.
Execute this line before parsing to clean it up:
str = str.replaceAll("(?<=^...)\\w+\\.?", "");
This will give up you Sep 11 2011 from Sept. 11 2011, but leave already appropriate input untouched. It will also clean up any length abbreviation, with or without the full stop - it's versatile.
You can then parse it using you existing format string.
This question already has answers here:
Date Format JAVA
(5 answers)
Closed 9 years ago.
I have a date in the following format
//input date
Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)
//output date format
I want to change this to "dd-mm-yyyy, hh:mm:ss".
I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.
I tried the following code.
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
try
{
Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
.format(date));
facade.setDate(date1);
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting
java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".
Any help..
"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead
But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so
new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
Here's some code which works for me:
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
Locale.ENGLISH);
Date date = inputFormat.parse(input);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
Locale.ENGLISH);
outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outputFormat.format(date);
System.out.println(output);
}
}
Things to consider:
You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.
This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.
Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
Date date = outputDate.parse("2013-06-06 00:00:00.0");
System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.
Fault is here
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.
#Test
public void test() throws ParseException {
SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
System.out.println(sdf_target.format(d));
}
output console : 2013-30-06 03:30:00.000
Here is my target format:
19 AUG 2011
And I try to convert this string to Calendar object by following code, but variable "date" remains null..
SimpleDateFormat formatter ;
Date date = null ;
formatter = new SimpleDateFormat("dd MMM yyyy");
try {
date = formatter.parse(returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal=Calendar.getInstance();
cal.setTime(date);
Does anyone know what's going wrong? Thank You.
FYI, exception msg:
Unparseable date: "19 Aug 2011" at java.text.DateFormat.parse(Unknown
Source)
But I don't think it is useful...
Something is going wrong with the parsing. You aren't finding out about it because of this:
catch (ParseException e) {
}
That's basically saying, "I don't care what goes wrong - ignore it." At the very least you should be logging the error, and more likely letting the exception bubble up.
Exceptions are an incredibly important diagnostic tool - don't just catch them and ignore them.
EDIT: Now that the question's changed, we can see the exception - but the code is still continuing as if nothing's happened. Even if you do want to mostly ignore the exception, you need to decide what value you want date to have if parsing failed. Clearly null is unhelpful - so you need to either let the exception bubble up (to let the caller know that parsing failed) or return some difference value (e.g. a default date, or today, or something like that).
Now, as it happens, letting the exception bubble up makes the code simpler too. It doesn't throw an exception on my machine, but maybe it will on yours:
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws Exception {
Calendar cal = parseReturnDate("19 AUG 2011");
System.out.println(cal);
}
public static Calendar parseReturnDate(String returnDate)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = formatter.parse(returnDate);
Calendar cal=Calendar.getInstance();
cal.setTime(date);
return cal;
}
}
Note how we don't need to declare variables separately to assigning them values, and that now we're letting the exception bubble up we can just assign date its useful value directly.
My guess is that your default time zone doesn't use "AUG" as a short month name - but I can't really tell without seeing the exception. If that's the case, you might want to specify the locale when constructing the formatter:
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy",
Locale.US);
You might also want to specify a time zone.
(As an aside, Joda Time is a far superior API for date and time handling. If you're doing any significant work with the value afterwards, I'd definitely recommend using it over Date/Calendar.)
The SimpleDateFormat depends on the current calendar and the calendar on the locale by default.
Depending the locale and the calendar probably "AUG" is not a valid text for the month.
AUG is a valid string for US Locale but for example not for ES Locale.
You can force the locale to US adding it to the SimpleDateFormat:
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy",Locale.US);
With the US locale you can parse correctly the string if the date has US 2 letter months.
I just tried your code and it works fine. Try running the following:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Playground {
public static void main(String... args) {
String returnDate = "19 AUG 2011";
SimpleDateFormat formatter ;
Date date = null ;
formatter = new SimpleDateFormat("dd MMM yyyy");
try {
date = formatter.parse(returnDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date.toString());
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println(cal.getTime().toString());
}
}
OUTPUT:
Fri Aug 19 00:00:00 CAT 2011
Fri Aug 19 00:00:00 CAT 2011
I need to parse the dates of the format "January 10th, 2010" in Java. How can I do this?
How to handle the ordinal indicators, the st, nd, rd, or th trailing the day number?
This works:
String s = "January 10th, 2010";
DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
System.out.println("" + dateFormat.parse(s.replaceAll("(?:st|nd|rd|th),", "")));
but you need to make sure you are using the right Locale to properly parse the month name.
I know you can include general texts inside the SimpleDateFormat pattern. However in this case the text is dependent on the info and is actually not relevant to the parsing process.
This is actually the simplest solution I can think of. But I would love to be shown wrong.
You can avoid the pitfalls exposed in one of the comments by doing something similar to this:
String s = "January 10th, 2010";
DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
System.out.println("" + dateFormat.parse(s.replaceAll("(?<= \\d+)(?:st|nd|rd|th),(?= \\d+$)", "")));
This will allow you to not match Jath,uary 10 2010 for example.
I should like to contribute the modern answer. Rather than the SimpleDateFormat class used in the two top-voted answer today you should use java.time, the modern Java date and time API. It offers a couple of nice solutions.
Easy solution
We first define a formatter for parsing:
private static final DateTimeFormatter PARSING_FORMATTER = DateTimeFormatter.ofPattern(
"MMMM d['st']['nd']['rd']['th'], uuuu", Locale.ENGLISH);
Then we use it like this:
String dateString = "January 10th, 2010";
LocalDate date = LocalDate.parse(dateString, PARSING_FORMATTER);
System.out.println("Parsed date: " + date);
Output is:
Parsed date: 2010-01-10
The square brackets [] in the format pattern string enclose optional parts, and the single quotes enclose literal text. So d['st']['nd']['rd']['th'] means that there may be st, nd, rd and/or th after the day of month.
More solid solution
A couple of limitations with the approach above are
It accepts any ordinal indicator, for example 10st and even 10stndrdth.
While the formatter works for parsing, you cannot use it for formatting (it would give January 10stndrdth, 2010).
If you want better validation of the ordinal indicator or you want the possibility of formatting the date back into a string, you may build your formatter in this way:
private static final DateTimeFormatter FORMATTING_AND_PARSING_FORMATTER;
static {
Map<Long, String> ordinalNumbers = new HashMap<>(42);
ordinalNumbers.put(1L, "1st");
ordinalNumbers.put(2L, "2nd");
ordinalNumbers.put(3L, "3rd");
ordinalNumbers.put(21L, "21st");
ordinalNumbers.put(22L, "22nd");
ordinalNumbers.put(23L, "23rd");
ordinalNumbers.put(31L, "31st");
for (long d = 1; d <= 31; d++) {
ordinalNumbers.putIfAbsent(d, "" + d + "th");
}
FORMATTING_AND_PARSING_FORMATTER = new DateTimeFormatterBuilder()
.appendPattern("MMMM ")
.appendText(ChronoField.DAY_OF_MONTH, ordinalNumbers)
.appendPattern(", uuuu")
.toFormatter(Locale.ENGLISH);
}
This will parse the date string the same as the one above. Let’s also try it for formatting:
System.out.println("Formatted back using the same formatter: "
+ date.format(FORMATTING_AND_PARSING_FORMATTER));
Formatted back using the same formatter: January 10th, 2010
Links
Oracle tutorial: Date Time explaining how to use java.time.
My answer to a question about formatting ordinal indicators from which I took the more solid formatter.
You can set nd etc as literals in a SimpleDateFormat. You can define the four needed format and try them. Starting with th first, because I guess this will occur more often. If it fails with ParseException, try the next one. If all fail, throw the ParseException. The code here is just a concept. In real-life you may would not generate the formats new everytime and may think about thread-safety.
public static Date hoolaHoop(final String dateText) throws ParseException
{
ParseException pe=null;
String[] sss={"th","nd","rd","st"};
for (String special:sss)
{
SimpleDateFormat sdf=new SimpleDateFormat("MMMM d'"+special+",' yyyy");
try{
return sdf.parse(dateText);
}
catch (ParseException e)
{
// remember for throwing later
pe=e;
}
}
throw pe;
}
public static void main (String[] args) throws java.lang.Exception
{
String[] dateText={"January 10th, 2010","January 1st, 2010","January 2nd, 2010",""};
for (String dt:dateText) {System.out.println(hoolaHoop(dt))};
}
Output:
Sun Jan 10 00:00:00 GMT 2010
Fri Jan 01 00:00:00 GMT 2010
Sat Jan 02 00:00:00 GMT 2010
Exception in thread "main" java.text.ParseException: Unparseable date: ""
"th","nd","rd","st" is of course only suitable for Locales with english language. Keep that in mind. In france, "re","nd" etc I guess.
This is another easy way ,but need to include apache commons jar.
import org.apache.commons.lang.time.*;
String s = "January 10th, 2010";
String[] freakyFormat = {"MMM dd'st,' yyyy","MMM dd'nd,' yyyy","MMM dd'th,' yyyy","MMM dd'rd,' yyyy"};
DateUtils du = new DateUtils();
System.out.println("" + du.parseDate(s,freakyFormat));