0

many times I've tried to figure out how to parse different date formats. On the input we have some lines:

1987-03-23
null
1985-11-29
23-07-2000
17-10-1984

It should be translated into the Russian(As in my task, you can change it) format dd.MM.yyyy and where it does not match the pattern to draw a dash.

I decided so:

public class DateUtil {
public static String validDateString(String strDate) {
final List<String> dateFormats = Arrays.asList("yyyy-MM-dd", "dd-MM-yyyy", "MM-dd- 
yyyy", "MM/dd/yyyy", "dd/MM/yyyy");
SimpleDateFormat sdf;
//Our new format
final String RU_FORMAT = "dd.MM.yyyy";
String output = "-";
//Reviewing for all templates
for (String format : dateFormats) {
    sdf = new SimpleDateFormat(format, new Locale("ru"));
    //Do not try to analyze dates that do not match the format
    sdf.setLenient(false);
    try {
        if (sdf.parse(strDate) != null) {
            Date date = sdf.parse(strDate);
            sdf.applyPattern(RU_FORMAT);
            return sdf.format(date);
        }
        break;
    } catch (ParseException e) {

    }
 }
 //Display line with a dash
 return output;
 }
}

Call this method you can by this line:

DateUtil.validDateString(input_line);
AlexS
  • 918
  • 1
  • 12
  • 28

1 Answers1

2

Avoid legacy date-time classes

The old Date, Calendar, and SimpleDateFormat classes are an awful sour mess of poor design. Never use them. Now supplanted by the java.time classes.

Parsing

First test if input string equals "null".

Next, parse the first format with simply LocalDate.parse( "1987-03-23" ) and trap for exception. That standard ISO 8601 format is handled by default, so no need to specify a formatting pattern.

Lastly, define a formatting pattern DateTimeFormatter.ofPatten( "dd-MM-uuuu" ) and parse with that, calling LocalDate.parse( input , formatter ). Trap for exception.

If all those fail, you have unexpected inputs.

Generating strings

Once you have a LocalDate object in hand, generate a string using LocalDate.format where you pass a DateTimeFormatter. You can define a formatting pattern as seen above. Or, you can call DateTimeFormatter.ofLocalizedDate to let java.time automatically localize for you.

Search Stack Overflow

This has been covered many many times already. Search Stack Overflow for these class names, to see more detail and more code.

I suggest using a search engine with site:stackoverflow.com criteria as the built-in search feature in Stack Overflow is weak and tends to ignore Answers, biasing towards only Questions.

For example, do this:

https://duckduckgo.com/?q=site%3Astackoverflow.com+java+DateTimeFormatter&t=h_&ia=web


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154