Home > Mobile >  Comparing dates with months as strings
Comparing dates with months as strings

Time:12-06

I am trying to compare these two dates :

17 Oct. 2019 (08:23)
19 déc. 2019 (21:15)

The months are in French and the main problem is the months. Do I need to put an if statement for every type of month so I can switch it with the appropriate month? For example:

if (MonthValue.equals("oct."){
    DateValue.replace("oct.","10");
}

Or is there an easier solution, because I need to check in a table if the first value is bigger than the second one.

Edit : My new Code :

String target1 = "17 oct. 2019 (08:23)";
        String target2 = "19 déc. 2019 (21:15)";
        DateFormat df = new SimpleDateFormat("dd MMM. YYYY (kk:mm)", Locale.FRENCH);
        Date result =  df.parse(target1);  
        Date result2 =  df.parse(target2); 
        System.out.println(result);
        System.out.println(result2);
        if(result.compareTo(result2) < 0) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }

Doesn't work gives this error:

java.text.ParseException: Unparseable date: "17 oct. 2019 (08:23)"

CodePudding user response:

Using DateTimeFormatter with pattern dd MMM yyyy (HH:mm) to parse the date string like this

String target1 = "17 oct. 2019 (08:23)";
String target2 = "19 déc. 2019 (21:15)";

Locale locale = Locale.FRANCE;
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().appendPattern("dd MMM yyyy (HH:mm)")
        .toFormatter(locale);
LocalDateTime dateTime1 = LocalDateTime.parse(target1, dateTimeFormatter);
LocalDateTime dateTime2 = LocalDateTime.parse(target2, dateTimeFormatter);

System.out.println(dateTime1);
System.out.println(dateTime2);
if (dateTime1.compareTo(dateTime2) < 0) {
    System.out.println("true");
} else {
    System.out.println("false");
}

CodePudding user response:

My recommendation:

CodePudding user response:

What I've got from your question is that you want to convert Month's names to their respective numbers. If this is the case, then you should try switch
Example:

switch (MonthValue){
    case jan:
      MonthValue = 1; // Set Month variable to it Numbered Position (type casting might be required)
      break;
    case feb:
      MonthValue = 2;
      break;
    default:
      System.out.println("Somthing...");
}

CodePudding user response:

Try encoding to UTF8, which will avoid DateTimeParseException Exception in thread "main" java.time.format.DateTimeParseException: Text '19 d??c. 2019 (21:15)' could not be parsed at index 3

    public static void main(String args[]) throws Exception {
        String date1 = "17 oct. 2019 (08:23)";
        String date2 = "19 déc. 2019 (21:15)";
        
        DateTimeFormatter longDateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy (HH:mm)").withLocale(Locale.FRENCH);
        LocalDateTime lclDate1 = LocalDateTime.parse(encodeUTF8(date1), longDateTimeFormatter);
        LocalDateTime lclDate2 = LocalDateTime.parse(encodeUTF8(date2), longDateTimeFormatter);
        if (lclDate1.compareTo(lclDate2) < 0) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
    
    public static String encodeUTF8(String dateStr) {
        byte[] bytes = dateStr.getBytes();
        String utf8EncodStr = new String(bytes, StandardCharsets.UTF_8);
        return utf8EncodStr;
    }
  • Related