I want to check if a given string is in a particular dateformat. If not user should be able to throw error message.
DateTimeFormat format=DateTimeFormat.getFormat("yyyy-MM-dd");
Date d= format.parse("1990-10-");
with this input , it should give error. I tried with try and catch , but its not throwing any exception.
Use the class
http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/shared/DateTimeFormat.html#parse(java.lang.String)
Which is in shared package.
Not the clinet package class
http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html#parse(java.lang.String)
Use java.text.SimpleDateFormat, it throws ParseException.
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d= format.parse("1990-10-");
} catch (ParseException e) {
...
}
Related
I am trying to create a service that does a series of operations on a database and then returns a set of information.
Among these information there's the birthdate of a series of customers.
When I try to parse (and then format) this dates I get the error in the title.
The strange thing is that even though the exception is raised, the code still compiles and runs without problems giving me the results I expect...
This is the method:
public String getDataNascitaFormattata() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date data = null;
try {
data = sdf.parse(dataNascita);
dataNascita = new SimpleDateFormat("dd/MM/yyyy").format(data);
} catch (ParseException e) {
System.err.println(e);
}
return dataNascita;
}
And this is an example:
Initial birthdate: "1969-09-07 00:00:00.0"
Desired birthdate: 07/09/1969
Error: java.text.ParseException: Unparseable date: "07/09/1969"
Result (WITH the exception being thrown):
EDIT: I've already tried adding the locale but the exception is still getting thrown...
EDIT2: here is a pic that explains in a better way the situation
i guess dataNascita is already a String '07/09/1969' and that's why the parse fails but the desired value is returned and the program works.
So you are trying to parse a String into an object and then re-format it in the same format as the original String!
Please run the following snippet and check your data
String dataNascita = "1969-09-07 00:00:00.0";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date data = null;
try {
data = sdf.parse(dataNascita);
dataNascita = new SimpleDateFormat("dd/MM/yyyy").format(data);
} catch (ParseException e) {
System.err.println(e);
}
System.out.println(dataNascita);
dataNascita = "07/09/1969";
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
data = null;
try {
data = sdf.parse(dataNascita);
dataNascita = new SimpleDateFormat("dd/MM/yyyy").format(data);
} catch (ParseException e) {
System.err.println(e);
}
System.out.println(dataNascita);
I am assuming dataNascita is a field (which seems useless).
You catch the exception and just print it to the stderr. The programming therefore continues and returns the last value assigned to dataNascita
edit:
You most likely want to do one the following
throw an exception
catch the exception elsewhere
Assign a "n/a"-kind of value to the dataNascita
You should also wonder why you are using a field for dataNascita (assuming this is the case based on the code).
I would like to parse String to Date. The problem is that if I parse the wrong date like "2009-02-40" I don't get any exception (no feedback that I passed wrong date) instead I get Date object set to "2009-01-01".
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date result = df.parse("2009-02-40");
System.out.println(result);
} catch (ParseException e) {
e.printStackTrace();
}
How to get an exception when I pass the wrong Date like this one above?
Try following code:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
df.setLenient(false); //note the change here
try {
Date result = df.parse("2009-02-40");
System.out.println(result);
} catch (ParseException e) {
e.printStackTrace();
}
You want to call setLenient(false) on your formater. This causes "strict" checking when parsing takes places. By default, "lenient" is "true; and then some heuristics are used that turn "garbage in" into whatever.
Probably not the best design in the world; but that is how it works.
df.parse("2009-02-40"); will throw ParseException, if the beginning of the specified string cannot be parsed.
For a strict parsing, use df.setLenient(false);
I have this simple program I've written
try{
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
sdf.setLenient(false);
//Date date = sdf.parse("1/14/1999"); Apologies for confusion
Date date = sdf.parse(request.getParameter("selectedDate"));
}catch(ParseException ex){
ex.printStackTrace();
}
From what I understand a ParseException will be thrown if the date is out of range or the format given is wrong. I want to be able to tell them apart. How can I can achieve this ?
Edit: When I said out of range I meant something like this 15/15/1999. That's why setLenient(false)
ParseException doesn't offer a reliably way of determining the cause of the exception itself. You could invoke parse twice setting lenient to true and false and checking its state in the exception block
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
try {
sdf.setLenient(true);
Date date = sdf.parse("1/33/1999");
System.out.println("DateFormat is OK");
sdf.setLenient(false);
date = sdf.parse("1/33/1999");
} catch (ParseException ex) {
ex.printStackTrace();
if (!sdf.isLenient()) {
System.out.println("Invalid date");
} else {
System.out.println("Invalid date pattern");
}
}
format given is wrong.
By that if you mean that the format is invalid, then that throws IllegalArgumentException, see here.
But you should not have to check that, the pattern you supply is determined at compile time and you should be able to ensure that it is valid; the check should be required only if the pattern is not known at compile time.
I read the doc and see that different exceptions are thrown in the two cases, so you can differentiate based on the exception class:
try {
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
sdf.setLenient(false);
Date date = sdf.parse("1/14/1999");
} catch (IllegalArgumentException ex) {
// format exception
} catch (ParseException ex) {
// parse exception
}
Kindly have a look at these Methods .I am not good at exceptions but maybe it gives you some help.
try{
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
sdf.setLenient(false);
Date date = sdf.parse("1/14/1999");
}catch(ParseException ex){
ex.printStackTrace();
System.out.println(ex.toString());//get cause and message ,localized details
System.out.println(ex.etErrorOffset()); //get position of error
}
I'm trying to access a static method in a Utils class I created:
public class Utils{
public static Date convertToDate(String dateString, String dFormat){
SimpleDateFormat dateFormat = new SimpleDateFormat(dFormat, Locale.US);
Date convertedDate;
try {
convertedDate = dateFormat.parse(dateString);
Log.i("date", "convertedDate = " + convertedDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
return convertedDate;
}
}
When I attempted to access this method like so:
Utils.convertToDate("03-04-2012", "mm-dd-yyyy");
I get a null pointer exception.
How could this be???
My guess is that it's not that method that's throwing the exception - but the fact that it returns null and you're using the return value, like this:
Date date = Utils.convertToDate("03-04-2012", "mm-dd-yyyy");
System.out.println(date.toString());
That's the problem with effectively swallowing exceptions and pretending nothing's wrong. Note that your format should be "MM-dd-yyyy" instead of "mm-dd-yyyy". Also note that your code would be simpler if you declared convertedDate within your try block and just returned it, instead of waiting to come out of the try block before returning.
(Having said all of this, I wouldn't have expected that code to throw an exception. It wouldn't give you the value you wanted, but it should be okay to actually parse... If you could produce a short but complete program demonstrating the problem, that would really help.)
Use this instead:
Utils.convertToDate("03-04-2012", "MM-dd-yyyy");
since mm represents the minutes, while MM represents the month.
I need to format a date(yyyyMMDD) into YYYY-MM-DD. Can I create a date format of the latter ?
Yes.
Use SimpleDateFormat.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
String sourceDateStr = "20101012";
try {
Date sourceDate = sourceFormat.parse(sourceDateStr);
String targetDateStr = targetFormat.format(sourceDate);
System.out.println(targetDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gives the output 2010-01-12
You can use a pre-written class like the pojava http://www.pojava.org/ for this.
It's very good a recognising different date formats and translating between them.
For example, to translate between the formats above:
try {
System.out.println("Date:" + new DateTime("Date").toString("yyyy-MM-dd"));
} catch (Exception error) {
error.printStackTrace();
}
I find that using this class helps as I don't have to worry too much about the different date formats that exist out there (except between UK/US dd/MM/yyyy & MM/dd/yyyy, which you can specifically configure the class for)
Have a look at joda-time: http://joda-time.sourceforge.net/