0

I have a string with six numbers: 650310. It represents 1965 march 10 in YYMMDD format.

Is there any method to recognize this format to 10 march 1965? Currently this is my method of doing which isn't very effective.

public class Example {

    public static void main(String args[]) {
        //date in YYMMDD

        //String x = "650310";
        String x = "161020";
        System.out.print(x.substring(4, 6)+" ");

        if (Integer.parseInt(x.substring(2, 4)) == 10) {
            System.out.print("October"+" ");
        }
        else  if (Integer.parseInt(x.substring(2, 4)) == 03) {
            System.out.print("March"+" ");
        }
        if (Integer.parseInt(x.substring(0, 2)) > 50) {
            String yr = "19" + x.substring(0, 2);
            System.out.println(yr);
        } else if (Integer.parseInt(x.substring(0, 2)) < 50) {
            String yr = "20" + x.substring(0, 2);
            System.out.println(yr);
        }
    }
}

output : 20 October 2016
BeyondProgrammer
  • 893
  • 2
  • 15
  • 32

4 Answers4

3

Use Java's SimpleDateFormat:

SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );

Now you have a Date object which you can use to display the date in other formats:

SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );

Use output.toString() to display your newly formatted date. Good luck.

mvreijn
  • 2,807
  • 28
  • 40
  • this is close, after fixing the error it gave me 20-10-2016 i want the 10 to be October – BeyondProgrammer Nov 05 '13 at 08:28
  • 1
    @user2822351 How about looking up the JavaDoc of `SimpleDateFormat? You will find tons of format parameters there. –  Nov 05 '13 at 08:30
  • Did you try after my last edit? That one gives you the month in text. Remember, that is localized according to the JVM's locale. – mvreijn Nov 05 '13 at 08:30
1

try this example

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Example {

    public static void main(String args[]) throws ParseException {

        SimpleDateFormat s = new SimpleDateFormat( "yyMMdd" );
        Date theDate = s.parse( "650310" );
        SimpleDateFormat p = new SimpleDateFormat( "dd MMMMM yyyy" );
System.out.println(p.format(theDate));
    }
}

OUTPUT 10 March 1965

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Use SimpleDateFormat for date parsing. For example:

SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
        System.out.println(format.parse("900310"));
} catch (ParseException e) {
    e.printStackTrace();
}

Output: Sat Mar 10 00:00:00 MSK 1990

EDIT: if you want to parse date,try to use DateFormat to get Date !!!! And then you can format it in your own way. I disagree with your downvote.

SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
    try {
        Date parse = format.parse("900310");
        format.applyPattern("dd MMMM yyyy");
        System.out.println(format.format(parse));
    } catch (ParseException e) {
        e.printStackTrace();
    }

output 10/Март/1990

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

This link will help. Create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings.

Community
  • 1
  • 1
Abhishek Agarwal
  • 846
  • 4
  • 13
  • 34