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

@Override
public Date parse(String text, ParsePosition pos) 

Source Link

Document

Parses text from a string to produce a Date.

Usage

From source file:edu.usu.sdl.openstorefront.common.util.TimeUtil.java

public static Date fromString(String value) {
    Date newDate = null;/*from   www. java  2 s . c o m*/
    if (StringUtils.isNotBlank(value)) {
        SimpleDateFormat sdf = new SimpleDateFormat(OMP_DATE_FORMAT);
        newDate = sdf.parse(value, new ParsePosition(0));
    }
    return newDate;
}

From source file:Main.java

/** Returns a Date object from a String. */
public static Date parseDate(String dateStr) {
    String formatString = "";

    if (dateStr.length() == 16)
        dateStr = dateStr.substring(0, 14);
    if (dateStr.length() == 15)
        formatString = "yyyyMMdd'T'hhmmss";
    if (dateStr.length() == 8)
        formatString = "yyyyMMdd";

    SimpleDateFormat formatter = new SimpleDateFormat(formatString);
    ParsePosition pos = new ParsePosition(0);

    return formatter.parse(dateStr, pos);
}

From source file:Main.java

public static Date getNowDateShort() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(currentTime);
    ParsePosition pos = new ParsePosition(8);
    Date currentTime_2 = formatter.parse(dateString, pos);
    return currentTime_2;
}

From source file:Main.java

public static String timeAgo(String iso8601) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    ParsePosition position = new ParsePosition(0);
    Date date = null;/*from   w  w  w. j  a  v  a 2  s .com*/
    try {
        date = simpleDateFormat.parse(iso8601.replaceFirst("Z", "+00:00"), position);
    } catch (Exception e) {
        System.out.println("could not parese date");

    }
    String timeAgo = date == null ? "" : DateUtils.getRelativeTimeSpanString(date.getTime()).toString();
    return timeAgo;
}

From source file:Main.java

public static Date getNowDate() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String dateString = formatter.format(currentTime);
    ParsePosition pos = new ParsePosition(8);
    Date currentTime_2 = formatter.parse(dateString, pos);
    return currentTime_2;
}

From source file:com.sunchenbin.store.feilong.core.text.DateFormatUtil.java

/**
 * ?.//from   w  w  w . j av  a2 s.  c o  m
 *
 * @param dateString
 *            the date string
 * @param pattern
 *            the pattern
 * @param locale
 *            the locale
 * @return the date
 * @see SimpleDateFormat
 * @see SimpleDateFormat#parse(String)
 * @see SimpleDateFormat#parse(String, ParsePosition)
 */
public static Date parse(String dateString, String pattern, Locale locale) {
    if (Validator.isNullOrEmpty(dateString)) {
        throw new NullPointerException("param dateString can not NullOrEmpty");
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
    ParsePosition parsePosition = new ParsePosition(0);
    return simpleDateFormat.parse(dateString, parsePosition);
}

From source file:edu.usu.sdl.openstorefront.common.util.Convert.java

/**
 * Attempts to convert several different formats
 *
 * MM/dd/yyyy HH:mm:ss z yyyy-mm-dd HH:mm:ss z yyyy-MM-dd'T'HH:mm:ss.SSS'Z
 * MM/dd/yyyy yyyy-mm-dd date standard milliseconds
 *
 * @param dateString/*from w  w w.java2s  .c  o  m*/
 * @return date
 */
public static Date toDate(String dateString) {
    Date dateConverted = null;

    if (StringUtils.isNotBlank(dateString)) {

        try {
            if (StringUtils.isNumeric(dateString)) {
                dateConverted = new Date(Long.parseLong(dateString));
            } else {

                String formats[] = { "MM/dd/yyyy HH:mm:ss z ", "yyyy-mm-dd HH:mm:ss z ",
                        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z", "MM/dd/yyyy", "yyyy-mm-dd ", };

                for (String format : formats) {
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    dateConverted = sdf.parse(dateString, new ParsePosition(0));
                    if (dateConverted != null) {
                        break;
                    }
                }
            }

        } catch (Exception e) {
            dateConverted = null;
        }
    }

    return dateConverted;
}

From source file:Main.java

public static String stringDateToString(String format, String dateString) {
    ParsePosition position = new ParsePosition(0);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
    SimpleDateFormat StringDateFormat = new SimpleDateFormat(format);
    Date dateValue = null;/*ww w . java  2  s  . c o  m*/
    String data = "";
    try {
        dateValue = simpleDateFormat.parse(dateString, position);
        data = StringDateFormat.format(dateValue);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return data;
}

From source file:Main.java

public static String stringDateToString(String format, String toFormat, String dateString) {
    ParsePosition position = new ParsePosition(0);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
    SimpleDateFormat StringDateFormat = new SimpleDateFormat(toFormat);
    Date dateValue = null;//from  www . j  a  v a  2 s. c  om
    String data = "";
    try {
        dateValue = simpleDateFormat.parse(dateString, position);
        data = StringDateFormat.format(dateValue);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return data;
}