Example usage for java.text SimpleDateFormat parse

List of usage examples for java.text SimpleDateFormat parse

Introduction

In this page you can find the example usage for java.text SimpleDateFormat parse.

Prototype

public Date parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a date.

Usage

From source file:Main.java

public static String convertUtc2Local(String utcTime) {
    String time = "";
    if (utcTime != null) {
        // 2014-10-24T02:58:05.932Z
        SimpleDateFormat utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.CHINA);
        utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date gpsUTCDate = null;//w  ww  .  j a va2s . c o  m
        try {
            gpsUTCDate = utcFormatter.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat localFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        localFormatter.setTimeZone(TimeZone.getDefault());
        assert gpsUTCDate != null;
        time = localFormatter.format(gpsUTCDate.getTime());
    }
    return time;
}

From source file:Main.java

public static String getRelativeTimeAgo(String rawJsonDate) {
    String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
    SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
    sf.setLenient(true);//  w  w w  .  j  a v  a  2 s  . c o  m

    String relativeDate = "";

    Date date;
    try {
        date = sf.parse(rawJsonDate);
        relativeDate = getTimeString(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return relativeDate;
}

From source file:Main.java

public static Date parseTimestamp(String timestamp) {
    for (SimpleDateFormat format : ACCEPTED_TIMESTAMP_FORMATS) {
        // TODO: We shouldn't be forcing the time zone when parsing dates.
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {//from  w w w . jav  a 2 s . co  m
            return format.parse(timestamp);
        } catch (ParseException ex) {
            continue;
        }
    }

    // All attempts to parse have failed
    return null;
}

From source file:Main.java

public static String getDaysWithoutYear(String srcDate) {

    SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat formater = new SimpleDateFormat("MM/dd");

    Date date = null;//from   ww w  .java  2 s.com
    try {
        parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        date = parserSDF.parse(srcDate);
    } catch (ParseException e) {
        Log.d(TAG, e.getMessage());
    }

    return formater.format(date);
}

From source file:Main.java

public static Date parseResponseDate(String date) {
    Date returnDate = null;//from   w w w.j  a v a 2 s. c  o m
    SimpleDateFormat format = null;
    for (int i = 0; i < DATETIME_FORMATS.length; ++i) {
        try {
            format = DATETIME_FORMATS[i];
            synchronized (format) {
                returnDate = format.parse(date);
            }
            return returnDate;
        } catch (ParseException e) {
            // this is not the format
        }
    }
    return null;
}

From source file:Main.java

public static final Date stringToDate(String paramString1, String paramString2, boolean paramBoolean) {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2, Locale.getDefault());
    localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
    try {/*from  w  w w. j a v a2  s .  co m*/
        Date localDate = localSimpleDateFormat.parse(paramString1);
        return localDate;
    } catch (Throwable localThrowable) {
        if (!paramBoolean) {
            return new Date(1000L + System.currentTimeMillis());
        }
    }
    return null;
}

From source file:com.esofthead.mycollab.core.utils.DateTimeUtils.java

public static Date convertDateByString(String strDate, String format) {
    if (!StringUtils.isEmpty(strDate)) {
        try {//from   w  w  w  . j av a 2s.c  o m
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.parse(strDate);
        } catch (ParseException e) {
            LOG.error("Error while parse date", e);
        }
    }
    return new Date();
}

From source file:Main.java

public static String string2Timezone(String srcFormater, String srcDateTime, String dstFormater,
        String srcTimeZoneId, String dstTimeZoneId) {
    if (srcFormater == null || "".equals(srcFormater))
        return null;
    if (srcDateTime == null || "".equals(srcDateTime))
        return null;
    if (dstFormater == null || "".equals(dstFormater))
        return null;
    if (dstTimeZoneId == null || "".equals(dstTimeZoneId))
        return null;
    SimpleDateFormat sdf = new SimpleDateFormat(srcFormater);
    try {/*from  ww w.  ja v  a 2s .c o m*/
        int diffTime = getDiffTimeZoneRawOffset(srcTimeZoneId, dstTimeZoneId);
        Date d = sdf.parse(srcDateTime);
        long nowTime = d.getTime();
        long newNowTime = nowTime - diffTime;
        d = new Date(newNowTime);
        return date2String(dstFormater, d);
    } catch (ParseException e) {
        return null;
    } finally {
        sdf = null;
    }
}

From source file:Main.java

public static Date format(String date, boolean fullTime) {
    SimpleDateFormat sdf = null;
    if (fullTime) {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    } else {/*from   w w  w. j a v a 2 s .  c  o m*/
        sdf = new SimpleDateFormat("yyyy-MM-dd");
    }
    try {
        Date d = sdf.parse(date);
        return d;
    } catch (ParseException e) {
    }
    return null;
}

From source file:irille.pub.svr.DateUtils.java

/**
 * Date//from w  ww  .ja  v a  2  s.  c om
 * @param timeMillis
 * @return
 * @throws ParseException
 */
public static Date timeMillisToDate(Long timeMillis) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String sd = format.format(new Date(timeMillis));
    Date date = format.parse(sd);
    return date;
}