Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format changing

If input is 01-01-2015 it should change to 2015-01-01.
If input is 2015-01-01 it should change to 01-01-2015.
I used SimpleDateFormat but didn't get the correct output:

//Class to change date dd-MM-yyyy to yyyy-MM-dd and vice versa
public class ChangeDate {
  static SimpleDateFormat formatY = new SimpleDateFormat("yyyy-MM-dd");
  static SimpleDateFormat formatD = new SimpleDateFormat("dd-MM-yyyy");

  //This function change dd-MM-yyyy to yyyy-MM-dd
  public static String changeDtoY(String date) {
    try {
      return formatY.format(formatD.parse(date));
    }
    catch(Exception e) {
      return null;
    }
  }

  //This function change yyyy-MM-dd to dd-MM-yyyy
  public static String changeYtoD(String date) {
    try {
      return formatD.format(formatY.parse(date));
    }
    catch(Exception e) {
      return null;
    }
  }
}

I want some condition that automatically detects the date's pattern and change to the other format.

like image 510
Sreemat Avatar asked Dec 25 '15 19:12

Sreemat


People also ask

Why does Excel keep changing my date format?

Microsoft Excel is preprogrammed to make it easier to enter dates. For example, 12/2 changes to 2-Dec. This is very frustrating when you enter something that you don't want changed to a date. Unfortunately there is no way to turn this off.

How do I stop Excel from auto formatting dates?

Click File > Options. In the Excel Options box, click Proofing > AutoCorrect Options. On the AutoFormat As You Type tab, check the boxes for the auto formatting you want to use.

Why is Excel not recognizing dates?

However, Excel may sometimes not correctly recognize dates when pasting using the 'Paste Special' option. To solve this issue, force the cells into Text format before you put dates into them: 1. Copy the dates from the Excel spreadsheet as they are (most likely they are in the "Date" format).


2 Answers

There are 2 options:

  1. Try to check with regular expression sth. like:

    if (dateString.matches("\\d{4}-\\d{2}-\\d{2}")) {
        ...
    }
    
  2. Try to convert to first pattern, if it throws exception, try to convert to another pattern (but it is bad practice to do so)

like image 198
Azat Nugusbayev Avatar answered Oct 19 '22 07:10

Azat Nugusbayev


Regex Is Overkill

For date-time work, no need to bother with regex.

Simply attempt a parse with one format, trapping for the expected exception. If the exception is indeed thrown, attempt a parse with the other format. If an exception is thrown, then you know the input is unexpectedly in neither format.

java.time

You are using old troublesome date-time classes now supplanted by the java.time framework built into Java 8 and later. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Oracle Tutorial.

LocalDate

The new classes include one, LocalDate, for date-only values without time-of-day. Just what you need.

Formatters

Your first format may be the standard ISO 8601 format, YYYY-MM-DD. This format is used by default in java.time.

If this first parse attempt fails because the input does not match ISO 8601 format, a DateTimeParseException is thrown.

LocalDate localDate = null;  
try {
    localDate = LocalDate.parse( input );  // ISO 8601 formatter used implicitly.
} catch ( DateTimeParseException e ) {
    // Exception means the input is not in ISO 8601 format.
}

The other format must be specified by a coded pattern similar to what you are doing with SimpleDateFormat. So if we catch the exception from the first attempt, make a second parse attempt.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-yyyy" );
LocalDate localDate = null;  
try {
    localDate = LocalDate.parse( input );
} catch ( DateTimeParseException e ) {
    // Exception means the input is not in ISO 8601 format.
    // Try the other expected format.
    try {
        localDate = LocalDate.parse( input , formatter );
    } catch ( DateTimeParseException e ) {
        // FIXME: Unexpected input fit neither of our expected patterns.
    }
}
like image 40
Basil Bourque Avatar answered Oct 19 '22 07:10

Basil Bourque