1

I have some problem with parse some information from String line. In this example, I would like to work with the line 2011-08-28 19:02:30. I have a lot of lines for this template. How to parse this date? Thanks

 [47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://example.com

Thanks all. Here is my solution.

 private Date parseDate(String line) {
    line = line.replaceAll("\\s+", " ").trim();
    String[] masWords = line.split(" ");
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
   try {
        date = format.parse(masWords[3] + " " + masWords[4]);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Yahor Urbanovich
  • 733
  • 1
  • 9
  • 17

3 Answers3

3

i'd do it like that:

   String[] s1 = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX".split(" ");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 1; i< s1.length ; i++  ){
            try {
                Date date = format.parse(s1[i-1]+" "+s1[i]);
                System.out.println("Date found: "+s1[i-1]+" "+s1[i]);
                break;
            } catch (ParseException e) {
                continue;
            }
        }
  1. Split the String
  2. loop through the String Array
  3. cast to date until you found a valid date.

if the you know the date is always on the same place in the string you can do it much easier by you cast:

String[] s1 = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX".split(" ");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.parse(s1[6]+" "+s1[7]);
osanger
  • 2,276
  • 3
  • 28
  • 35
3

You need to normalize your string by removing the extra spaces... () Regex can do that fast and easy), then split the string, concatenate the elements of the string that you need for building a date, parse those to a Date object Woala...

a snippet for this:

String chain = "[47.611910999999999, -122.335178]  6   2011-08-28 19:02:30 I'm at Saigon Vietnamese Restaurant II (1529 6th Ave., at Pine St., Seattle) http://t.co/8s86hNX";
        chain = chain.replaceAll("\\s+", " ").trim();
        System.out.println(chain);
        String[] var = chain.split(" ");
        for (String string : var) {
            System.out.println(string);
        }

        String string = var[3] + " " + var[4];
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(string);
        System.out.println(date);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2
  1. Split with a space as the delimiter String[] parts = str.split(" ");
  2. Loop through parts[] to find items matching your date and time.
  3. You can use the following regexes:
    • Date: \d{4}-\d{2}-\d{2}
    • Time: \d{2}:\d{2}:\d{2}
  4. You can work with detected strings containing your time and date using java.util.Date
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183