0

I have two date formats in Strings i.e:

1981-12-07 and 07-11-1991

String one_date = "1981-12-07";
String another_date = "07-11-1991";

and I need to convert them in one format.

For example in "yyyy-MM-dd";

All I want is to insert this strings correctly into sqlite database so it all should be the same format, ok?

java SimpleDataFormat eats everything without checking.

Substring is not suitable here. Can someone tell me the method of checking?

alex111
  • 63
  • 1
  • 1
  • 6
  • Which format would you like to keep? – Spikatrix May 29 '15 at 09:38
  • I doubt there is **the** (one) method of checking, do you mean SimpleDat**e**Format? – Binkan Salaryman May 29 '15 at 09:40
  • possible duplicate of [How do I convert the date from one format to another date object in another format without using any deprecated classes?](http://stackoverflow.com/questions/12503527/how-do-i-convert-the-date-from-one-format-to-another-date-object-in-another-form) – Sachin Gupta May 29 '15 at 09:40
  • I believe it would be better you explain what you want to achieve. Because converting `07-11-1991` into `1991-11-07` seems not what you want. – SubOptimal May 29 '15 at 09:41
  • @BinkanSalaryman there is one method :). it works 99% of the time – nafas May 29 '15 at 09:58

4 Answers4

1

You need to write a code that will distinguish the date version before parsing. You need to write this code on your own based on some characteristics of the Strings you would like to parse. For those two, I would do

if (one_date.split("-")[2].length()==2) {
    // parse it with yyyy-MM-dd
} else {
 // parse it with dd-MM-yyyy or something
}

You could also use .lastIndexOf('-'); for checking where the last '-' is, it would be better for the performance

And of course we can use .charAt(somePositionIndex) to check if the '-' sign is where we expect to find it.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

You can do something like this:

    SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat secondFormat = new SimpleDateFormat("dd-MM-yyyy");
    String one_date = "1981-12-07";
    String another_date = "07-11-1991";
    String[] strs = new String[] { one_date, another_date };
    ArrayList<Date> dates = new ArrayList<Date>();
    for (String s : strs) {
        try {
            if (s.matches("\\d{4}-\\d{2}-\\d{2}")) {
                dates.add(defaultFormat.parse(s));
            } else {
                dates.add(secondFormat.parse(s));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    ArrayList<String> converted = new ArrayList<String>();
    for (Date d : dates) {
        converted.add(defaultFormat.format(d));
    }
    System.out.println(converted);
Titus
  • 22,031
  • 1
  • 23
  • 33
0

I you have many different date format, I suggest you to use natty

Here is an example of how to use it to return a date. once you have this date you can simply format it in anyway you want.

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;
import java.util.Date;

public class ParseDate {
   public static Date parse(String date)throws Exception{
        Parser parser = new Parser();
        List<DateGroup> groups =  parser.parse(date);


        int year;
        int day=1;
        int month=1;
        for(DateGroup group : groups){
            for( Date d : group.getDates()){
                year=d.getYear();
                month=d.getMonth();
                day=d.getDay();
                Date fixedDate=new Date(year,month,day);
                return fixedDate;
            }
        }

        throw new Exception("unparsable date");
 }
}
nafas
  • 5,283
  • 3
  • 29
  • 57
0

The other answers are overly complicated.

Trap For Parsing Exception

Simple try parsing using one format, trapping for an Exception caused by a mismatching input. If the exception is thrown, try the other format.

Also, avoid the java.util.Date/.Calendar classes. They are notoriously troublesome, and are now supplanted by the java.time package and/or the Joda-Time library.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154