0

I want to convert date like 94 NOV 21 to 94/11/21 using SimpleDateFormat in java.

i created a simple date format

 new SimpleDateFormat("yy MMM dd") 

to parse 94 NOV 21 . but it is not parsable what is the correct pattern?

In my code i have many standard formats of date which i am handling by adding all of them into a list (of DateFormats) and comparing the incoming date with this list and i am handling the non standard date (R 250051Z FEB 99)within the exception block which is working fine but my main problem is i am unable to parse 94 NOV 21 with new SimpleDateFormat("yy MMM dd") .Last System.out statement goes to the exception block but it should be handled with new SimpleDateFormat("yy MMM dd") but this is not working.

  import java.util.List;
  import java.util.Locale;
  import java.io.UnsupportedEncodingException;

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

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.StandardCharsets;

import org.apache.commons.lang.StringUtils;

     public class Test2 {

   public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println(getDate("August 21 99"));

    System.out.println(getDate("21 August 1999"));

    System.out.println(getDate("94 NOV 21"));

     }

   private static String getDate(String inputString) {

    if (!StringUtils.isEmpty(inputString)) {
        String input = inputString.replaceAll("[^A-Za-z0-9 ]", " ");
        // System.out.println("input to formatter>>" + input);
        String requiredDate = "";
        SimpleDateFormat requiredDateFormat = new        

      SimpleDateFormat("MM/dd/yy");

        List<SimpleDateFormat> dateFormats = new    

    ArrayList<SimpleDateFormat>() {
            {

                add(new SimpleDateFormat("MMM dd yyyy", Locale.US));
                add(new SimpleDateFormat("MMM dd yy", Locale.US));
                add(new SimpleDateFormat("yy MMM yy", Locale.US));
                add(new SimpleDateFormat("dd MMM yyyy", Locale.US));
                add(new SimpleDateFormat("M dd yyyy", Locale.US));
                add(new SimpleDateFormat("dd M yyyy", Locale.US));
                add(new SimpleDateFormat("dd MM yy", Locale.US));
                add(new SimpleDateFormat("MM dd yy", Locale.US));
                add(new SimpleDateFormat("M dd yyyy hh mm ss a", 

    Locale.US));
                add(new SimpleDateFormat("dd M yyyy hh mm ss a", 
     Locale.US));

            }
        };

        Date date = null;

        for (SimpleDateFormat format : dateFormats) {
            // System.out.println("in for loop");
            try {
                format.setLenient(false);
                date = format.parse(input.trim());
                if (date != null) {
                    requiredDate =          
    requiredDateFormat.format(date).toString();
                    break;
                }
            } catch (ParseException e) {

                String day = inputString.subSequence(2, 4).toString();
                String monYear = inputString.substring(10);
                String dateToConvert = day + " " + monYear;
                SimpleDateFormat customDateFormatter = new SimpleDateFormat("dd MMM yy");
                try {
                    date = customDateFormatter.parse(dateToConvert);
                    if (date != null) {
                        requiredDate = requiredDateFormat.format(date).toString();
                        break;
                    }

                } catch (Exception ex) {

                    requiredDate = null;

                }
                // System.out.println("Handle separately");

            }

        }

        System.out.println("requiredDate>>>" + requiredDate);
        return requiredDate;

    }

    else {
        return null;
    }
   }

  }

2 Answers2

1

Your format works fine on Ideone.

I suspect you are working in a non-English default locale. Try forcing the date to be parsed in the US locale:

System.out.println(  // Works.
    new SimpleDateFormat("yy MMM dd", Locale.US).parse("94 NOV 21"));

System.out.println(  // Does not work - expects NOV. (with a period).
    new SimpleDateFormat("yy MMM dd", Locale.FRANCE).parse("94 NOV 21"));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

UPDATE:

well after seeing the code you updated then I need to advise you dont comming forward, you are using bruteforce to parse a given string into a date object, wich in some cases can work but in others will just corrupt your data and break down the hole application...

Imagine what can happen if this.

04 12 05 is trying to be parsed?

is that 4th December 2005? 12th May 2004, April 12 2005? well the answer is, is a Russian-roulette because you are iterating the array list with multiple formatters...

to summarise:

you NEED need to know the date format. period.


Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Why Spanish, specifically? – Andy Turner May 04 '16 at 15:16
  • I think the author meant that a [Romance Language](https://en.wikipedia.org/wiki/Romance_languages) was in use, with Spanish and French being examples of Romance languages. (Though I do not know what was meant by "ita", "Italian" perhaps?) The example of "Nov" may be from any of many languages, especially the Romance languages as you can see in [this list of translated month names](http://www.omniglot.com/language/time/months.htm). – Basil Bourque May 04 '16 at 16:24
  • Exactly... @basil bourque not just because the first 3 letters are the same for all this languages, but the format ist almost a standard 94Nov21... – ΦXocę 웃 Пepeúpa ツ May 04 '16 at 16:28