Hi i get the following error:
2014-09-26T14:17:40.779-0300|Grave: 'java.lang.NumberFormatException' recebido ao invocar escuta de a��o '#{comentario.cancelarAtendimento}' para o componente 'j_idt140'
2014-09-26T14:17:40.780-0300|Grave: java.lang.NumberFormatException: For input string: ""
i created this formatter :
DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("pt", "BR"));
and i want to retrive the date from the session in this way :
if (sessao.getAttribute("dataInicial") != null)
{
dataInicial = (String) sessao.getAttribute("dataInicial");
dataIni = new java.sql.Date(formatter.parse(dataInicial).getTime());
}
then i pass the dataIni here
ocorrencias = cadastradorOcorrecia.pesquisarAvancada(usuario.getCodigo(), Integer.parseInt(pesqCodigo), pesquisaCliente, pesquisaStatus, pesquisaDepartamento, pesquisaSolicitante, pesquisaUltimoAtend, pesquisaSistema, dataIni, dataFi, pesquisaCriador,Integer.parseInt(pesquisaProduto),Integer.parseInt(pesquisaModulo));
here i get the numberFormatException ---> Can anyone have an idea of what i´m doing wrong?
Thanks in advance
and then it goes to the dao instance for retrive the information with the database.
The Integer.parseInt throws NumberFormatException if it cannot convert a string into integer. In your case I would check the values of pesqCodigo, pesquisaProduto & pesquisaModulo.
JavaDoc for Integer.parseInt(String s)
Examples
Integer.parseInt(" 234"); ---> Throws NumberFormatException as it has space
Integer.parseInt(""); ---> Throws NumberFormatException as it is not parseable for an integer
Integer.parseInt(null); ---> Throws NumberFormatException and not npe
Integer.parseInt(" 234 ".trim()); ---> Doesnt throw NumberFormatException as it trims before trying to parse it.
Edit:
It is not surprising that it does not work. This would probably work better:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print with your required format you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Notes:
you should include the locale as if your locale is not English, the day name might not be recognised
IST is ambiguous and can lead to problems so you should use the proper time zone name if possible in your input.
At least one of pesqCodigo, pesquisaProduto or pesquisaModulo is a String that does not represent a number.
I think there is no relation with dataInicial, as a DateFormat shouldn't throw a NumberFormatException. The Integer.parseInt() methods can throw this Exception, so just have a look at the use of that function.
Related
I'm trying to convert JSON to Java Object (transaction) and vice versa.
I keep getting this exception:
java.text.ParseException: Unparseable date: "Sun Apr 28 02:41:11 IDT 2019"
at this line in my code below:
timeReceived = sdf.parse(json.get("timeReceived").toString());
even though I read the Date class description a lot of times and the format I
used should match the date received.
Would appreciate some assistance. thanks!
My code:
public Transaction convertJsonToTransaction(JSONObject json){
UUID uuid= UUID.fromString(json.getAsString("uuid"));
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z
yyyy");
Date timeReceived= null;
try {
timeReceived = sdf.parse(json.get("timeReceived").toString());
} catch (ParseException e) {
e.printStackTrace();
}
String recipient =json.get("recipient").toString();
Date timeSent= null;
try {
timeSent = sdf.parse(json.get("timeSent").toString());
} catch (ParseException e) {
e.printStackTrace();
}
String description=json.getAsString("description");
return new Transaction(uuid, sender, timeReceived, recipient,
timeSent, description);
}
public static void main (String args[]){
Transaction t = new Transaction(UUID.randomUUID(), "ms1", new
Date(), "ms2", new Date(), "flow");
net.minidev.json.JSONObject jo = t.convertTransactionToJson();
System.out.println(((JSONObject) jo).toString());
Transaction tr = t.convertJsonToTransaction(jo);
System.out.println(tr.toString());
}
You have a really small error in the date format: an extra space between date and hour placeholder EEE MMM d HH:mm:ss z yyyy. Get rid of that and it works EEE MMM d HH:mm:ss z yyyy
You have \n in regex. This thing happened many times with me. Just a suggestion when you found this type of issue go to any online tool to check special or hidden character in your string (Some time hidden char char will be there, you can not found them using your eyes.). Also one thing If you copy regex or any string used in program from any tool or online or any doc.., before pasting it in your code check if you copied any special char or hidden char. I normally use this tool.
Every thing else is correct It is working for me.
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I am trying to convert dates of the format
07/Mar/2004:16:56:39 -0800
to a date object. I'm not sure what that format's name even is but its used in tomcat access logs. Can someone please help me out?
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy:HH:mm:ss");
Date d = f.parse("07/Mar/2004:16:56:39 -0800"); // Throws exception.
System.out.println(d.getTime());
The format string should match the input. In particular, the separator must match.
Also, your format string is missing the time zone part to match against the -0800.
Since your input uses English month name, you should explicitly specify that, e.g. using Locale.US.
SimpleDateFormat f = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
Date d = f.parse("07/Mar/2004:16:56:39 -0800");
System.out.println(d);
Since I'm in Eastern time zone, that prints:
Sun Mar 07 19:56:39 EST 2004
You should however use the new java.time classes instead.
Since the input string has a time zone offset, that means you should parse the string to an OffsetDateTime, using a DateTimeFormatter:
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MMM/uuuu:HH:mm:ss Z", Locale.US);
OffsetDateTime dt = OffsetDateTime.parse("07/Mar/2004:16:56:39 -0800", f);
System.out.println(dt);
Output is:
2004-03-07T16:56:39-08:00
You need to add the time zone to your date format and change the format to your input string (/ instead of -):
SimpleDateFormat f = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
See the docs: https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You have a typo in the line:
Date d = f.parse("07/Mar/2004:16:56:39 -0800");
The format for the date is "dd-MMM-yyyy:HH:mm:ss". You need to replace the "/" with "-". Additionally, you need to surround your parse function with a try-catch block as follows:
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy:HH:mm:ss");
Date d;
try {
d = f.parse("07-Mar-2004:16:56:39");
System.out.println(d.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Check out this link to learn more about the SimpleDateFormat Class:
http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142
I have a date value of the type "2013-03-28T15:16:58.000Z". I want to convert it to "dd-MMM-yyyy" format. For that, I have used the following code:
public static String getTime(String time)
{
try
{
String tim = time.replace("T", " ");
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.US);
SimpleDateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
return df2.format(df1.parse(tim));
}
catch (ParseException e)
{
return null;
}
}
I got this solution from a variety of posts here in StackOverflow. But this code still returns null. Can anyone tell me why?
The null value is being returned from exception block due the first SimpleDateFormat returning null as a result of an invalid date format.
The Zpattern is used to denote timezone patterns. To accept a literal Z character, you need to surround the character with single quotes.
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'", Locale.US);
Also, you can do the same with the T character if you don't wish to do a manual replace:
Instead of
String tim = time.replace("T", " "); // remove
just use time and use:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
It is returning null because it is catching a ParseException and you are telling it to return null. You should be at least print out the stacktrace and that should tell you why you are getting the ParseException.
It's throwing a ParseException parsing the string 2013-03-28 15:16:58.000Z.
Try getting rid of the Z [like you did with the T] and using a SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)
I am trying to convert a String value that is stored in a database,for example "2012-01-20", to be in the format January 20, 2012.
I have seen some examples, but they are using Date which works with SimpleDateFormat.
As an example here is one way I tried but the "try" always fails and the result is null
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate=null;
try {
convertedDate = df.parse(datePlayed);
} catch(ParseException e){
e.printStackTrace();
}
In short, you're not using the right format for parsing. You need to use two DateFormat instances; one for parsing and one for formatting.
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
Date convertedDate = parser.parse(datePlayed);
String output = formatter.format(convertedDate);
Date da = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E, dd/MM/yyyy !");
System.out.println("Update : " + ft.format(da));
You can change your date style do you want at: E, dd/MM/yyyy !
Good luck !
If the Date is read from a Database, then store it as a java.sql.Date. You can then use SimpleDateFormat on it, maybe after converting to java.util.Date. From the ResultSet object, you can extract Dates.
If what you meant is that you are given a date in text that was extracted from a DB by someone else and you are stuck with it. Try using:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(true);
convertedDate = df.parse(datePlayed.trim() );
Also try displaying the text you are parsing before you parse to make sure the datePlayed value is what you expect.
With parseInt, an extra space before or after the data will cause an error, so calling trim() removes extra spaces.
I am reciving a input in this format 2012-01-13T00:00:00.000-05:00 and which i need to convert this into yyyyMMdd Format .
I have also set the SimpleDateFormat.setLenient(false);
This is my coding for parsing the Date
public static String getparsedDate(String date) throws Exception {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
sdf.setLenient(false);
String s1 = date;
String s2 = null;
Date d;
try {
d = sdf.parse(s1);
s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return s2;
}
But i am getting a Exception at
java.text.ParseException: Unparseable date: "201201"
at java.text.DateFormat.parse(Unknown Source)
Could anybody please let me know , what might be the issue ?
You are missing the timezone in your format string. If you check the argument, it is finishing with -05:00 and you are also using Lenient==false.
Unfortunately, the time zone formats available to SimpleDateFormat are not ISO8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC822. Therefore using SimpleDateFormat does not seem as an option in your case (since you use UTC−05:00 as timezone).
Instead of SimpleDateFormat you need to use JodaTime for that type of date format.