-5

I Am passing date as string Input: "8 Aug 2016"

I want output as format of above date i.e.

Output: DD MMM YYYY

Can anyone help here?

Priya P
  • 123
  • 1
  • 4
  • 11
  • if they always come in this form, split on spaces as separator. that's the "hardest" part – Stultuske Aug 11 '16 at 10:35
  • 1
    Am I understanding correctly? What you want is: for any date *in any format* the output would be the format of the Date? I assume the month will always be in letters and the year always in four digits, otherwise how would you tell the difference between the day, the month and the year? – Ray O'Kalahjan Aug 11 '16 at 10:43
  • The issue has been resolved now, thanks for all your answers. – Priya P Oct 13 '16 at 05:27

6 Answers6

2

You can try using a fixed number of predefined date formats and then use SimpleDateFormat to parse the date to test against. Use the first one that doesn't throw an exception.

Note however that there's no way to do this with complete certainty. There are simply too many alternatives, and they are often ambiguous (consider e.g. MM/dd/yyyy versus dd/MM/yyyy).

marthursson
  • 3,242
  • 1
  • 18
  • 28
1

tl;dr

LocalDate.parse( "8 Aug 2016" , DateTimeFormatter.ofPattern ( "d MMM uuuu" ).withLocale ( Locale.US ) )

java.time

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

Now in maintenance mode, the Joda-Time project also advises migration to java.time.

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

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

Define a formatting pattern. Specify the Locale defining the human language of the name of the month. If omitted the JVM’s current default Locale is implicitly applied. That default can vary. Better to specify your desired/expected Locale explicitly.

String input = "8 Aug 2016";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "d MMM uuuu" );
f = f.withLocale ( Locale.US );

LocalDate ld = LocalDate.parse ( input , f );
String output = ld.format ( f );

Dump to console.

System.out.println ( "input: " + input + " | ld: " + ld + " | output: " + output );

input: 8 Aug 2016 | ld: 2016-08-08 | output: 8 Aug 2016

Of course your parsing code should trap for DateTimeParseException being thrown. Omitted here for brevity.

ISO 8601

I strongly recommend passing around date-time strings in the standard formats of ISO 8601. For a date-only value, that would be YYYY-MM-DD.

String input = "2016-08-08" ;
LocalDate ld = LocalDate.parse( input );
String output = ld.toString();  // "2016-08-08"
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
public static void main(String[] args) throws ParseException {
        String time = "8 Aug 2016";
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
        Date date = sdf.parse(time);// this parses your string into a date.
        System.out.println(sdf.format(date));//this will format your date in the format you specify.
}

here is a nice tutorial on converting string objects to date. link

Abhishek
  • 2,485
  • 2
  • 18
  • 25
0
new SimpleDateFormat("dd MMM yyyy").format(yourDateObject);
beeb
  • 1,615
  • 1
  • 12
  • 24
0

Have a look at java.text.SimpleDateFormat. It allows you to specify formats for both parsing and output formatting.

import java.text.SimpleDateFormat;

public class DateDemo {
    public static void main(String[] argv) {
        SimpleDateFormat parser = new SimpleDateFormat("d MMM yyyy");
        try {
            java.util.Date date = parser.parse("8 Aug 2016");
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            System.out.println(formatter.format(date));
        } catch (java.text.ParseException e) { // won't happen here
            System.err.println("Invalid date");
        }
    }
}
Lars
  • 325
  • 4
  • 5
0

Instead of fetching the date format, I compared the actual date with date of required format (by converting actual date in that format) as below.

 public static boolean isValidFormat(String format, String value) {
  Date date = null;
  try {
   SimpleDateFormat sdf = new SimpleDateFormat(format);
   date = sdf.parse(value);
   if (!value.equals(sdf.format(date))) {
    date = null;
    return true;
   }
  } catch (ParseException ex) {
   ex.printStackTrace();
  }
  return false;
 }

Here I am passing two parameters to the method isValidFormat() , one is the format of the date I need to validate as String format and String value as value of date and then performing the format check of both dates (one parsed in required format and actual value) and returning the result accordingly.

Priya P
  • 123
  • 1
  • 4
  • 11