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:com.fluke.util.Util.java

public static Date getDateFromMarketTime(String date) throws ParseException {
    String filteredDate = date.replaceAll("-", "").trim();
    SimpleDateFormat formatter = new SimpleDateFormat(MARKET_TIME_FORMATTER);
    return formatter.parse(filteredDate);
}

From source file:Main.java

public static String convert2LocalTime(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = null;/*from www  . j a v a  2s . co  m*/
    try {
        d = sdf.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return output.format(d);
}

From source file:Main.java

public static Date convertStrToDate(String dateStr) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
    Date date = null;/*  www . j a va  2 s . c  o  m*/
    try {
        date = dateFormat.parse(dateStr);
    } catch (Exception e) {
        date = null;
    }
    return date;
}

From source file:com.daimler.spm.storefront.util.QuoteExpirationTimeConverter.java

/**
 * Converts quote expiration time from {@link String} to {@link Date} by adjusting the time part to end of day
 * (23:59:59).//from www  .j  a  v  a 2 s . com
 *
 * @param date
 *           the string representation of quote expiration time
 * @param pattern
 *           the date pattern to be used for conversion
 * @param locale
 *           the locale to be used for conversion
 * @return null if the string representation of expiration time is null or empty, otherwise the {@link Date} object
 */
public static Date convertStringToDate(final String date, final String pattern, final Locale locale) {
    if (StringUtils.isEmpty(date)) {
        return null;
    }

    final SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, locale);

    try {
        return QuoteExpirationTimeUtils.getEndOfDay(dateFormat.parse(date));
    } catch (final ParseException e) {
        throw new IllegalArgumentException(
                String.format("Failed to parse date [%s] using [%s] parsing format.", date, pattern), e);
    }
}

From source file:Main.java

/**
 *  Convert date from String to Xml GregorianCalendar in this 'MM/dd/yyyy hh:mm:ss'
 * @param s/*from   ww  w.  j a v a2 s  . c  o  m*/
 * @return
 * @throws DatatypeConfigurationException
 * @throws ParseException
 */
public static XMLGregorianCalendar stringToXMLGregorianCalendar(String s)
        throws DatatypeConfigurationException, ParseException {
    if (s != null && !(s.trim().length() < 1)) {
        XMLGregorianCalendar result = null;
        Date date;
        SimpleDateFormat simpleDateFormat;
        GregorianCalendar gregorianCalendar;

        simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        date = simpleDateFormat.parse(s);
        gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
        gregorianCalendar.setTime(date);
        result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        return result;

    }
    return null;
}

From source file:com.autentia.common.util.DateFormater.java

public static Date parse(String value, FORMAT datetime) {
    Date date = null;/* w ww.ja va 2 s  .  co m*/
    if (StringUtils.isNotEmpty(value)) {
        final SimpleDateFormat format = new SimpleDateFormat(datetime.getSimpleDateformat());
        try {
            date = format.parse(value);
        } catch (ParseException e) {
            if (log.isTraceEnabled()) {
                log.trace(" La fecha no se corresponde con el formato esperado: " + value);
            }
        }
    }
    return date;
}

From source file:Main.java

public static long parseTimeToLong(String time) {
    if (null == time)
        return System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    java.util.Date d;//from w ww .  j  a  va  2 s .  com
    try {
        d = sdf.parse(time);
        return d.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return System.currentTimeMillis();
}

From source file:Main.java

public static Calendar toCalendar(String s, String format) {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
    s = s.replace("T", " "); // car T est invalide
    try {//from   w w  w  .j a v a 2 s .c o m
        c.setTime(simpleDateFormat.parse(s));
    } catch (ParseException e) {
        //          TODO Auto-generated catch block
        e.printStackTrace();
    }
    return c;
}

From source file:Main.java

public static Date getDateFromFormattedStringInGMT(String str) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = null;/*from w  w  w.  j av  a  2 s.co  m*/
    try {
        date = simpleDateFormat.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

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);/*from   www  .  j  a  v  a 2 s. c  o m*/

    String relativeDate = "";
    try {
        long dateMillis = sf.parse(rawJsonDate).getTime();
        relativeDate = DateUtils
                .getRelativeTimeSpanString(dateMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS)
                .toString();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // Shorten relative date
    relativeDate = relativeDate.replaceAll(" ", "").replace("seconds", "s").replace("second", "s")
            .replace("minutes", "m").replace("minute", "m").replace("hours", "h").replace("hour", "h")
            .replace("days", "d").replace("day", "d").replace("weeks", "w").replace("week", "w")
            .replace("months", "M").replace("month", "M").replace("years", "y").replace("year", "y")
            .replace("ago", "").replace("in", "").replace("0s", "just now");

    return relativeDate;
}