Clint L
Clint L

Reputation: 1113

How to reformat a string date in Java

I tacked this problem in VB awhile back, and thought I could easily translate it to Java. The input comes in as a string in the format: "mm/dd/yyyy" I want to change this to the following format: "mm/dd/yy" where the last two year digits are shown only. I wrote this VB awhile back, which does just that:

Function DateFormat(ByVal myDate As String) As String
    Dim reformat As Date
    reformat = Date.Parse(myDate, Nothing)
    Return Format(reformat, "MM/dd/yy").ToString()
End Function

How can I do this exact same thing in Java, so that the date is reformatted correctly and returned as the string it originally was? I have something like this but it is not working properly:

    public static String DateFormat(String myDate){
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        try{
            Date formattedDate = formatter.parse(myDate);
            return formattedDate.toString();
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

I am not sure how to make it the format I need, as I can't find anything similar to the Format() function VB has. Thanks in advance.

Upvotes: 2

Views: 1059

Answers (4)

Kasper Ziemianek
Kasper Ziemianek

Reputation: 1349

Try this :

  public static String DateFormat(String myDate) throws ParseException {
    SimpleDateFormat inFormat = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yy");    

    Date parsedInDate = inFormat.parse(myDate);
    return outFormat.format(parsedInDate);
  }

At start, we declare two date formatters, then we create Date object from input String, and at the end we produce String in new format.

Upvotes: 2

Matt T.
Matt T.

Reputation: 549

You've basically almost got it, just need to apply the new format.

public static String DateFormat(String myDate){
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    try{
        Date date = formatter.parse(myDate);
        formatter.applyPattern("MM/dd/yy");
        return formatter.format(date);
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

Upvotes: 0

Greg Hilston
Greg Hilston

Reputation: 2424

SimpleDateFormat takes in a number of different formats. I believe the format you want is already built in and can be accessed like so...

Date date = new Date();

Format formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
System.out.println(s);

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201477

If I understand your question, you could use a pair of SimpleDateFormat(s)

private static final String formatIn = "MM/dd/yyyy";
private static final String formatOut = "MM/dd/yy";
private static final DateFormat sdfIn = new SimpleDateFormat(
    formatIn);
private static final DateFormat sdfOut = new SimpleDateFormat(
    formatOut);

public static String formatDateString(String dateIn)
    throws ParseException {
  return sdfOut.format(sdfIn.parse(dateIn));
}

public static void main(String[] args) {
  try {
    System.out.println(formatDateString("07/15/2014"));
  } catch (ParseException e) {
    e.printStackTrace();
  }
}

Output is

07/15/14

Upvotes: 2

Related Questions