2

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.

Clint L
  • 1,093
  • 5
  • 12
  • 29
  • 2
    Hopefully [Customizing Data Format](http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html), might help :-) – nIcE cOw Jul 15 '14 at 15:04
  • i'm not into Java at all, but in .NET you can pass a "date format" argument to the `toString()` method of a date type so that you decide precisely the output format. – Laurent S. Jul 15 '14 at 15:05
  • 1
    possible duplicate of [Diplaying the last two digits of the current year in java](http://stackoverflow.com/questions/20070258/diplaying-the-last-two-digits-of-the-current-year-in-java) and [this](http://stackoverflow.com/q/5143763/642706) and [this](http://stackoverflow.com/q/251535/642706) and [this](http://stackoverflow.com/q/9034766/642706) – Basil Bourque Jul 15 '14 at 16:45
  • possible duplicate of [Convert String Date to String date different format](http://stackoverflow.com/questions/14999506/convert-string-date-to-string-date-different-format) – Raedwald Jul 15 '14 at 23:08

4 Answers4

2

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

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.

Kasper Ziemianek
  • 1,329
  • 8
  • 15
  • This exactly meets my requirements for the function without using the global variables. thanks xwid. – Clint L Jul 15 '14 at 15:25
  • This is essentially my answer. Here, you are instantiating temporary objects on every method call for constant DateFormat(s). – Elliott Frisch Jul 15 '14 at 15:34
0

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);
Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
0

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;
    }
}
Matt T.
  • 539
  • 3
  • 6