Tarun Verma
Tarun Verma

Reputation: 73

want to convert date in US format through java

I have the below program in which dates need to be transformed in US format dates as you can see are being entered by me but still they need to be transformed in US format which is not happening rite now

please advise how to achieve this below is my code

 public class DateFormattingTest {

    private static final SimpleDateFormat outputDate = new SimpleDateFormat(
            "dd/MM/yyyy");


    public static void main(String[] args) {

        System.out.println ("03/20/2020-->:" + extractDate("03/20/2020") );
         System.out.println ("2033-05-01-->:" + extractDate("2033-05-01") );
        System.out.println ("08-05-34-->:" + extractDate("08-05-34") );
         System.out.println ("30-DEC-2013 -->:" + extractDate("30-DEC-2013") );
         System.out.println ("DEC-31-2013 -->:" + extractDate("DEC-31-2013") );


    }

    public static Date extractDate(String dateStr) {

        String [] datePatternsUS = {"d-M-yy", "d-M-yyyy", "d/M/yy", "d/M/yyyy","dd-MM-yy", "yyyy-MM-dd", "dd-MMM-yy","dd-MMM-yyyy","dd-MM-yyyy",
                "dd/MM/yy","dd/MMM/yy","dd/MMM/yyyy"};

        Date date = null;

        try {
            date = DateUtils.parseDate(dateStr, datePatternsUS);
        }
        catch (Exception except) {
                except.printStackTrace();
        }
        return date;
    }


}

upon execution i get the below output.. which is not correct please advise how to correct it in US format

03/20/2020-->:Tue Aug 03 00:00:00 IST 2021
2033-05-01-->:Thu Nov 23 00:00:00 IST 2006
08-05-34-->:Mon May 08 00:00:00 IST 2034
30-DEC-2013 -->:Mon Dec 30 00:00:00 IST 2013
DEC-31-2013 -->:null
java.text.ParseException: Unable to parse the date: DEC-31-2013

Upvotes: 2

Views: 279

Answers (3)

Würgspaß
Würgspaß

Reputation: 4820

Use the formatter and return String from extractDate (and re-arrange the date formats in your array):

    private static final SimpleDateFormat outputDate = new SimpleDateFormat("dd/MM/yyyy");

    public static void main(String[] args) {
         System.out.println ("03/20/2020-->:" + extractDate("03/20/2020") );
         System.out.println ("2033-05-01-->:" + extractDate("2033-05-01") );
         System.out.println ("08-05-34-->:" + extractDate("08-05-34") );
         System.out.println ("30-DEC-2013 -->:" + extractDate("30-DEC-2013") );
         System.out.println ("DEC-31-2013 -->:" + extractDate("DEC-31-2013") );
    }

    public static String extractDate(String dateStr) {
        String [] datePatternsUS = { "MM/dd/yyyy", "yyyy-MM-dd", "dd-MM-yy", "dd-MMM-yyyy","MMM-dd-yyyy" };

        Date date = null;
        try {
            date = DateUtils.parseDate(dateStr, datePatternsUS);
            return outputDate.format(date);
        }
        catch (Exception except) {
                except.printStackTrace();
        }        
        return null;
    }

The problem is, however, that "yyyy-MM-dd", "dd-MM-yy" are conflicting patterns, so that (depending on order) either line 2 or 3 outputs a bogus date.

Output is:

03/20/2020-->:03/08/2021

2033-05-01-->:23/11/2006

08-05-34-->:08/05/2034

30-DEC-2013 -->:30/12/2013

DEC-31-2013 -->:31/12/2013

Upvotes: 1

giograno
giograno

Reputation: 1809

First, you should add the pattern MMM-dd-yyyy to your patterns. I've symplified your method extractDate() and I've added Locale.US. Now it returns all your input dates correctly.

static String [] formatStrings  = {"d-M-yy", "d-M-yyyy", "d/M/yy", "d/M/yyyy",
        "dd-MM-yy", "yyyy-MM-dd", "dd-MMM-yy","dd-MMM-yyyy","dd-MM-yyyy",
        "dd/MM/yy","dd/MMM/yy","dd/MMM/yyyy",
        "MMM-dd-yyyy"};

public static void main(String[] args) {

    System.out.println ("03/20/2020-->:" + extractDate("03/20/2020") );
    System.out.println ("2033-05-01-->:" + extractDate("2033-05-01") );
    System.out.println ("08-05-34-->:" + extractDate("08-05-34") );
    System.out.println ("30-DEC-2013 -->:" + extractDate("30-DEC-2013") );
    System.out.println ("DEC-31-2013 -->:" + extractDate("DEC-31-2013") );

}


public static Date extractDate(String dateString) {

    for (String formatString : formatStrings) {
        try
        {
            return new SimpleDateFormat(formatString, Locale.US).parse(dateString);
        }
        catch (ParseException e) {}
    }
    return null;
}

Upvotes: 1

Aakash
Aakash

Reputation: 1791

The issue is that you don't have any pattern matching the last format of date. Add this to your array.

MMM-dd-yyyy

Upvotes: 0

Related Questions