-1

Need some suggestions to fix my issue. I have date input coming in 2 format ways(input1 and input2).

String input1="12-1-2012 T 10:23:34";
String input2="20-10-2012 T 10:34:22";

String format = "dd-MM-yyyy T HH:mm:ss";

SimpleDateFormat sdf= new SimpleDateFormat(format);

How to handle input1 and input2 using sdf object to produce outcome as dd-MM-yyyy T HH:mm:ss

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Manu
  • 55
  • 2
  • 10

3 Answers3

5

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API.

You need two instances of DateTimeFormatter

You need two instances of DateTimeFormatter:

  1. One for parsing: DateTimeFormatter.ofPattern("d-M-uuuu 'T' HH:mm:ss"). Note that a single M can parse a month in the range 1-12 and a single d can parse a day in the range 1-31.
  2. Another for formatting: DateTimeFormatter.ofPattern("dd-MM-uuuu 'T' HH:mm:ss").

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("d-M-uuuu 'T' HH:mm:ss", Locale.ENGLISH);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu 'T' HH:mm:ss", Locale.ENGLISH);

        String[] arr = { "12-1-2012 T 10:23:34", "20-10-2012 T 10:34:22" };
        for (String s : arr) {
            LocalDateTime ldt = LocalDateTime.parse(s, parser);
            String output = ldt.format(formatter);
            System.out.println(output);
        }
    }
}

Output:

12-01-2012 T 10:23:34
20-10-2012 T 10:34:22

ONLINE DEMO

Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

You have to adjust your pattern:

  • use single-digit months of year (and maybe even days of month, hours of day, minutes of hour and seconds of minute)
  • escape the T by enclosing it in single quotes

You can do that the modern way (since Java 8) or the outdated way (not recommended).

Here are two examples, one for each way:

Modern way (java.time)

public static void main(String[] args) {
    String input1 = "12-1-2012 T 10:23:34";
    String input2 = "12-01-2012 T 10:23:34";
    // define your pattern)
    String format = "d-M-uuuu 'T' H:m:s";
    // define a DateTimeFormatter from the pattern
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
    
    // parse both of the Strings using the defined formatter
    LocalDateTime ldt1 = LocalDateTime.parse(input1, dtf);
    LocalDateTime ldt2 = LocalDateTime.parse(input2, dtf);
    // print a result
    System.out.println(ldt1 + " and " + ldt2 + " are " 
            + (ldt1.equals(ldt2) ? "equal" : "not equal"));
}

Output

2012-01-12T10:23:34 and 2012-01-12T10:23:34 are equal

Outdated way (java.util and java.text)

public static void main(String[] args) throws ParseException {
    String input1 = "12-1-2012 T 10:23:34";
    String input2 = "12-01-2012 T 10:23:34";
    // define your pattern
    String format = "d-M-yyyy 'T' H:m:s";
    try {
        // create a SimpleDateFormat from the pattern
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        // parse both of the Strings using the date format
        Date date1 = sdf.parse(input1);
        Date date2 = sdf.parse(input2);
        System.out.println(date1 + " and " + date2 + " are " 
                + (date1.equals(date2) ? "equal" : "not equal"));
    } catch (ParseException e) {
        e.printStackTrace();
        throw(e);
    }
}

Output

Thu Jan 12 10:23:34 CET 2012 and Thu Jan 12 10:23:34 CET 2012 are equal

Please note that the outdated way magically added a time zone, which is CET due to this code executed on my German machine…
For datetimes with zones or offsets, you will have to use different classes in java.time, such as ZonedDateTime and OffsetDateTime.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    Well described! A minor improvement: Never leave `e.printStackTrace();` without suggesting to use `throw(e);`. As a general rule, A method should always throw the exception if it can not handle it; so that the exception can be handled by the calling method. – Arvind Kumar Avinash Jan 19 '23 at 15:17
1

MM is the two-digit month format, using values 01 - 12. You probably want the single digit format M, which uses values 1 - 12.

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • I want to handle if month comes as 1 then it should be 01.. 10-1-2021 should be 10-01-2021 – Manu Jan 19 '23 at 12:27