-1

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):

enter image description here




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

enter image description here

Leon
  • 665
  • 4
  • 10
  • 32
  • sysout-ing `dataNascita` before parse gives the Initial birthdate you entered? – Apostolos Jul 08 '16 at 07:54
  • 1
    *even though the exception is raised, the code still compiles and runs without problems* ... That's rather contradictory. – Manu Jul 08 '16 at 07:55
  • @Manu still that's what happens... – Leon Jul 08 '16 at 07:59
  • Can you show the initia value of dataNascita? With the value "1969-09-07 00:00:00.0" in dataNascita is working fine. I guess the original value of dataNascita is a different one – SCouto Jul 08 '16 at 08:00
  • @Apostolos before parse dataNascita has the initial format (the one recovered from the service that I've written in the example) – Leon Jul 08 '16 at 08:01
  • @SCouto that's the exact initial value :S – Leon Jul 08 '16 at 08:01
  • I don't think so, I agree with the answer below, please, check your input String (i guess dataNascita is already a String '07/09/1969') – SCouto Jul 08 '16 at 08:30
  • I've updated the question with a picture... – Leon Jul 08 '16 at 08:37
  • I can run the same program with your code with result as "07/09/1969". I'm not sure whats the input. Coz the input "1969-09-07 00:00:00.0" works perfectly. – Karthik R Jul 08 '16 at 08:50
  • @LeonGuerrero please accept an answer. i guess you found out the error. – Apostolos Jul 08 '16 at 12:22

2 Answers2

2

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);
Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • wait.... dataNascita initially has this format: "1969-09-07 00:00:00.0" and after the parse/format it becomes this: "07/09/1969" – Leon Jul 08 '16 at 08:06
  • can you sysout it exactly before the parse method? it is weird because the exception is saying that it gets a string and a template and it cannot match them to create a `Date` object. – Apostolos Jul 08 '16 at 08:08
  • I've already done it... BEFORE the parse method it is 1969-09-07 00:00:00.0.... AFTER the parse/format it is 07/09/1969 – Leon Jul 08 '16 at 08:11
  • @LeonGuerrero run the snippet i just posted to see where the exception is thrown. sth is wrong with your dataNascita variable. first part executes fine. second throws exception. – Apostolos Jul 08 '16 at 08:15
  • @LeonGuerrero exactly but `dataNascita = "07/09/1969"` before parse! that's what i'm trying to point out :) – Apostolos Jul 08 '16 at 08:26
  • you told me that before the parse method the value was `1969-09-07 00:00:00.0` but according to the snippet i posted, that' not going to throw exception, so how is an exception thrown at your program? – Apostolos Jul 08 '16 at 08:28
  • so where is this date printed? is see the timestamp date, the exception and the short date being printed? were is this last line printed? maybe you call this method twice? – Apostolos Jul 08 '16 at 08:41
0

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).

raphaëλ
  • 6,393
  • 2
  • 29
  • 35
  • I'm not doing it purposely. I've made a rest service that calls some pl/sql procedures and the returns json objects as a response. A mapper (the one in which there's this date parsing method) then gets these objects and transform them into strings and creates an array of objects... I'm already throwing an exception :) – Leon Jul 08 '16 at 08:05
  • Where are you throwing the exception? You are catching (handling) and the program simply continues and returns the value of `dataNascita` which has the value it had before this method was called. – raphaëλ Jul 08 '16 at 08:08
  • what's the point in throwing an exception?! If I throw an exception at that point the execution will stop. I don't want to know if there's an exception. I'd like to know why there's an UNPARSEABLEDATEXCEPTION but I still get a date that's both parsed and formatted as I want...... – Leon Jul 08 '16 at 08:15
  • Throwing an exception (or passing it on) is one of the possibilities if you feel the handling of this exception should be within a different context (e.g, the UI). You are getting a parsed date as this is the "previous" value of your field (field means it is part of the state of the object, so you are saving it everytime to the object's state as apposed to a variable which is used per code-block) – raphaëλ Jul 08 '16 at 08:18