-2

I have a date in the format dd-mm-yyyy format(inside my db).I need to convert it to dd-mm-yy format.I retrieved date from db and stored it inside a string.

String date=doc.getString("date");

for example: 05-19-1990

to
    05-19-90.

That 1990 is not fully needed only 90 is needed.Using split() in java i tried some thing but it wont helps me.Can anyone please help.Any help will be highly appreciated.......

Miller
  • 744
  • 3
  • 15
  • 39

4 Answers4

3

Use DateFormat for that issue:

DateFormat df = SimpleDateFormat("dd-MM-yyyy");
DateFormat df1 = SimpleDateFormat("dd-MM-yy");
df1.format(df.parse(date));
Jens
  • 67,715
  • 15
  • 98
  • 113
2

Try below code

 try {
        SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yy");
        Date d = sdf.parse("05-19-1990");
        System.out.println(sdf.format(d));
    } catch (ParseException ex) {
       ex.printStackTrace();
    }
Keval
  • 1,857
  • 16
  • 26
1

Use two SimpleDateFormats, one to parse it, one to format it..

SimpleDateFormat in = new SimpleDateFormat("MM-dd-yyyy");
Date inDate = in.parse(date); // 05-19-1990
SimpleDateFormat out = new SimpleDateFormat("MM-dd-yy");
String newDate = out.format(inDate);

or

SimpleDateFormat out = new SimpleDateFormat("dd-MM-yy");
String newDate = out.format(inDate);

If you really want 19-05-90 as per the title of your question ;)

Check java.text.SimpleDateFormat for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try this..

DateFormat df2 = new SimpleDateFormat("dd-mm-yy");
String formattedDate2 = df2.format(theDate);
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50