Java Parse Date parseDateString(String dateString)

Here you can find the source of parseDateString(String dateString)

Description

parse Date String

License

Apache License

Declaration

static public Date parseDateString(String dateString) 

Method Source Code

//package com.java2s;
/**//from ww w. ja v  a  2 s .  c  om
 *Copyright [2010] [David Hardtke]
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    static public Date parseDateString(String dateString) {
        Date d = null;
        d = parseBingDate(dateString);
        if (d != null)
            return d;
        d = parseBossDate(dateString);
        if (d != null)
            return d;
        d = parseTwitterDate(dateString);
        if (d != null)
            return d;
        d = parseVideoSurfDate(dateString);
        if (d != null)
            return d;
        d = parseCollectaDate(dateString);
        if (d != null)
            return d;
        d = parseUnixDate(dateString);
        if (d != null)
            return d;
        return d;
    }

    static public Date parseDateString(String dateString, DateFormat df) {
        if (dateString == null || df == null)
            return null;
        try {
            Date out = df.parse(dateString);
            return out;
        } catch (ParseException pe) {
            return null;
        }
    }

    static public Date parseBingDate(String dateString) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss'Z'");
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return parseDateString(dateString, df);
    }

    static public Date parseBossDate(String dateString) {
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return parseDateString(dateString, df);
    }

    static public Date parseTwitterDate(String dateString) {
        //   Tue, 22 Sep 2009 23:16:19 +0000   
        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
        //      DateFormat df = new SimpleDateFormat("EEE',' dd MMM yyyy kk:mm:ss Z");
        return parseDateString(dateString, df);
    }

    public static Date parseVideoSurfDate(String dateString) {
        if (dateString == null || !dateString.contains("ago"))
            return null;
        Date d = null;
        Calendar today = Calendar.getInstance();
        String[] splitString = dateString.split(" ");
        if (splitString.length > 1) {
            try {
                int number = Integer.parseInt(splitString[0]);
                String timeUnit = splitString[1];
                if (timeUnit.contains("yr")) {
                    today.add(Calendar.DAY_OF_YEAR, -(365 * number));
                } else if (timeUnit.contains("mo")) {
                    today.add(Calendar.DAY_OF_YEAR, -(30 * number));
                } else if (timeUnit.contains("da")) {
                    today.add(Calendar.DAY_OF_YEAR, -(number));
                } else if (timeUnit.contains("wk")) {
                    today.add(Calendar.DAY_OF_YEAR, -(7 * number));
                } else if (timeUnit.contains("hr")) {
                    today.add(Calendar.HOUR_OF_DAY, -number);
                } else if (timeUnit.contains("mi")) {
                    today.add(Calendar.MINUTE, -number);
                }
                return today.getTime();
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return d;
    }

    static public Date parseCollectaDate(String dateString) {
        DateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
        return parseDateString(dateString, df);
    }

    static public Date parseUnixDate(String dateString) {
        try {
            long date = Long.parseLong(dateString);
            return new Date(date);
        } catch (NumberFormatException nfe) {
            return null;
        }
    }
}

Related

  1. parseDateRFC(String value)
  2. parseDateRfc822(String str)
  3. parseDates(String query)
  4. parseDateStrFormat(String strDate)
  5. parseDateString(String dateStr)
  6. parseDateString(String pattern, String dateString)
  7. parseDateString(String s)
  8. parseDateString(TimeZone tz, String dateString)
  9. parseDateStringToDate(DateFormat format, String dateString)