0

I've a csv file with this form :

[csv file]

I don't arrive to parse this type of data. I try with a simple example but it doesn't work. However, I think my SimpleDateFormat is correct.

 Date date = null;
 String date1 ="22 févr. 17, 17:11";

     SimpleDateFormat formater = null;
     formater = new SimpleDateFormat("dd MMM YY , hh:mm ");
      try {
          date = formater.parse(date1);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
S.B
  • 7
  • 5
  • 1
    You have a space after the YY and the mm. That won't do. Also, use `yy`, not `YY` - it's a different type of year. – RealSkeptic Jan 22 '20 at 15:30
  • Thanks Frederic ! It work now with my simple example but not with my csv form, Indeed java return me this problem : Unparseable date: "19 f�vr. 18, 09:05", how can I fixe the UTF-8 encode ? – S.B Jan 22 '20 at 15:43
  • 2
    That's a different question. – RealSkeptic Jan 22 '20 at 15:45
  • 1
    No, that won’t always work. Why not? Basically because `SimpleDateFormat` is so confusing and troublesome. I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 22 '20 at 16:55
  • Please, when asking about code that doesn’t work, apart from desired result always incluce precise observed result. Quote any error message or stacktrace literally. In the latter case format the stacktrace as code for readability and tell us which line in your code the stacktrace refers to. – Ole V.V. Jan 27 '20 at 04:59
  • Related: [java parsing string to date](https://stackoverflow.com/questions/14546742/java-parsing-string-to-date). And [Comparing two times in android](https://stackoverflow.com/questions/20321711/comparing-two-times-in-android). – Ole V.V. Jan 27 '20 at 05:02

1 Answers1

2

Avoid legacy date-time classes

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

java.time

Use only classes from the java.time packages. For a date with time of day but lacking the context of a time zone or offset-from-UTC, that would be LocalDateTime.

Specify a Locale to determine the human language used in translation, and the cultural norms used in deciding issues of abbreviation, punctuation, capitalization, order of parts, and so on.

String input = "1 mars 17, 20:21" ;
DateTimeFormatter f = 
        DateTimeFormatter
        .ofPattern( "d MMM uu, HH:mm" )
        .withLocale( Locale.CANADA_FRENCH ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

These formatting codes are explained in the Javadoc. Study carefully. Case-sensitive.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Perfect, even the better `u` i.o. `y`. It is worth explaining that `h` is for 12 and `H` for 24 hours notation, especially with 20:21. – Joop Eggen Jan 22 '20 at 17:15
  • I was about to post similar code. Since we don’t know how hours before 10 look in the CSV file, I had put only one `H` in my format pattern string. It will accept both `3` and `03` for hour of day, for example. – Ole V.V. Jan 22 '20 at 18:02