-1

I have the following string variables in which dates are stored in format of dd/mm/yy that is UK format

String s= "04-MAR-15"
String t ="04/03/15"
String p ="04/03/2015"
String w ="04-03-2015"

now i want to convert their format in DD/MM/YYYY format, please advise how can I convert their format through simple date format in java and show on console.

Atri
  • 5,511
  • 5
  • 30
  • 40
naresh saxena
  • 47
  • 1
  • 1
  • 7

2 Answers2

0

The process is a two step process:

  • Convert the String to a Date using the parse method of DateFormat
  • Convert the Date to the new String using the method format of a DateFormat with the desired format

Use the SimpleDateFormat concrete class. See the documentation to understand the desired pattern.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 08 '17 at 03:11
0

Try parsing with each expected format

When you have inputs of various formats, if those formats are mutually exclusive (not ambiguous for one another), simply try parsing each known possible format until one works. When a parsing attempt fails, catch the Exception thrown and try parsing with the next format.

For more information on parsing a string into a date-only object, see How can I change the date format in Java? and many other posts already made on Stack Overflow. Here's a brief example of code.

First, define and collect a series of formatters for each expected format.

Locale locale = Locale.UK ;
List<DateTimeFormatter> formatters = new ArrayList<>( 4 );
formatters.add( DateTimeFormatter.ofPattern( "dd-MMM-uu" , locale ) ) ;
formatters.add( DateTimeFormatter.ofPattern( "dd-MM-uu" , locale ) ) ;
formatters.add( DateTimeFormatter.ofPattern( "dd/MM/uuuu" , locale ) ) ;
formatters.add( DateTimeFormatter.ofPattern( "dd-MM-uuuu" , locale ) ) ;

Try each formatter successively until one succeeds.

LocalDate ld = null ;
for( DateTimeFormatter f : formatters ) {
    try{
        ld = LocalDate.parse( input , f );
        break ; // End this 'for' loop. We succeeded in parsing, so no need to try more formatters. 
    } catch ( DateTimeParseException e ) {
        // Swallow this exception as we expect it when trying a series of formatters.
    }
}

Test to see if all the formatters failed to parse. This means we received an input of unexpected format, an error situation.

if( null == ld ) {  // All the formatters were tried but failed.
    … // Deal with unexpected input.
}

Generate a new String as your output using DateTimeFormatter. Consider using the ofLocalized… method rather than specify a particular format.

ISO 8601

Personally, I recommend against ever using a two-digit year. That causes no end of confusion being ambiguous with the month and day-of-month. Memory is cheap enough, and screens big enough, that we can easily afford the bits and pixels to support the luxury of 4-digit years.

Also, consider avoiding your patterns entirely. For data exchange, use only the standard ISO 8601 formats. For a date only that would be YYYY-MM-DD such as 2017-01-23.


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.

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.

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