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
Related
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.
I'm fairly new here. I'm used to working with C# but I'm a newbie in java.
I'm trying to get an hour String out of a jSpinner (Date model), but I keep getting errors. I've looked into some answers that are already given here... but it still doesn't work.
uurStr returns "Sat Jan 25 16:09:49 CET 2014", I'm trying to get "16:09" out of it. But no luck so far.... any ideas?
The exception is thrown on the "uurDate = sdf.parse(uurStr);" part.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat outputFmt = new SimpleDateFormat("HH:mm");
String uurStr = String.valueOf(jSpinner1.getValue());
Date uurDate = null;
try {
uurDate = sdf.parse(uurStr);
} catch (ParseException ex) {
Logger.getLogger(frmBackEnd.class.getName()).log(Level.SEVERE, null, ex);
}
String uur = outputFmt.format(uurDate);
JOptionPane.showMessageDialog(null, uur);
In order to correctly parse the weekday and timezone information, which both are language specific, it is necessary to set the locale as follows:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
If the locale is not set, then the default locale of the JRE is taken. This may be the OS default locale or a user specific locale or the locale may have been overridden by another Java class with Locale.setDefault(Locale.XXXX). In any of these cases, this may be the correct locale or not.
That said, it is usually safer to set the timezone or you may get wrong time information if the timezone is not available in the string to be parsed:
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
In your example the timezone is available and you don't have to worry about that.
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.
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
Can any one tell me solution for the following:
I have a Util.Date object in java.I want to validate the date entered. I am parsing the date object using the required format.
For ex, Format is "MM/dd/yyyy" and Date entered is "23/12/2010"
For this date, I am not getting any Parse Exception but the date is adjusted to "11/12/2011" which should not happen in my case.Instead I want to throw error message.
Please help me in this asap.
Thanks in advance
In DateFormat class you have to reset "lenient" flag, i.e.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
. . .
sdf.parse(mydate, pos);
You must set your "lenient" mode of the SimpleDateFormat to false:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
By default it is tolerant to some errors, and tries to interpret them somehow.
Depending upon your format expression , we can adjust month, date and year using SimpleDateFormat , code is below
public class SimpleDateFormatString2DateParse {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
try {
Date date = formatter.parse("11/12/2011");
System.out.println("Date is : " + date);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----output-----
Date is : Sat Nov 12 00:00:00 PST 2011