0

In My Application I am reading the date parameter from user in the String format.

After reading the parameter, I need to convert the String to *desired date format.*

Desired format = YYYY-MM-DD HH:MM:SS

Input String can be in any format.

Eg: 2013/09/19 14:21:07 OR 2013-09-19 14:21:07

Irrespective of the format Obtained, I need to convert it to desired format. How to achieve this?

I saw few snippet where,

SimpleDateFormat formatter = new SimpleDateFormat("YYYY/MM/DD HH:MM:SS"); 
Date dt = formatter.parse("2013/09/19 14:21:07");
SimpleDateFormat desiredFormatter = new
SimpleDateFormat("YYYY-MM-DD HH:MM:SS");

desiredFormatter .format(dt);

The above snippet works only when we know the input format to parse the String. But In my case I dont know the input format. So I was thinking to use directly format method without parsing.

Is this possible?

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
user001
  • 991
  • 6
  • 16
  • 34
  • Where are you getting the input string from, that you don't know the format? In which case, we really can't help. – Rohit Jain Sep 19 '13 at 11:25
  • 4
    The same way you convert any data for which you don't know the format. With magic. – Kayaman Sep 19 '13 at 11:26
  • 4
    The input string can be in any format? How are you going to know if `2013/08/05` means 5 August 2013 or 8 May 2013? – Jesper Sep 19 '13 at 11:27
  • Can't be done, you may be able to support multiple known formats, but you can't parse something which is of an unknown format. Even with multiple known formats, you need to make sure there is no ambiguity, or you deal with the ambiguity in a way that makes sense for your application. (Maybe if more than one format matches you give the user a choice, or only accept if a single format matches, or accept in an order of priority... – jasg Sep 19 '13 at 11:28
  • possible duplicate of [How to convert String to Date without knowing the format?](http://stackoverflow.com/questions/3707485/how-to-convert-string-to-date-without-knowing-the-format) – Hariharan Sep 19 '13 at 11:31
  • Most importantly, make it clear for the user which formats are supported. An input field that is labeled merely with "Date:" with no information about which format you should enter the date in is completely useless. – Alderath Sep 19 '13 at 11:38

4 Answers4

4

It's not possible. You cannot determine if 01/01/2013 is MM/dd/yyyy or dd/MM/yyyy if you dont know the real format beforehand

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

As you said date will be Eg: 2013/09/19 14:21:07 OR 2013-09-19 14:21:07

I think this way will be useful to you

String text="date in yyy/mm/dd or yyy-mm-dd"
SimpleDateFormat sdf1=null;
        try {
            if(text.contains("/"))
            {
               sdf1  = new SimpleDateFormat("yyyy/MM/dd");

            }
            else
            {
               sdf1  = new SimpleDateFormat("yyyy-MM-dd");

            }
            SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");

            Date date = sdf1.parse(text);

            System.out.println("Result==> " + sdf2.format(date));
        } catch (ParseException exp) {
            exp.printStackTrace();
        }
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Thanks Guys, For All your Comments and Answers.

Even I feel that it is not possible to convert String to Date without knowing its input format. Anyways I will try to rework on the design to change the way the Input is read.

Thanks Again.

user001
  • 991
  • 6
  • 16
  • 34
0

It depends on the Design.

1. If user can type any format in GUI side, then we can't able to convert, simply its impossible.
2. If user date format is validated with any specific format in GUI, then we can able to convert it With extra condition in server side coding.
Hariharan
  • 3,191
  • 4
  • 19
  • 31