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 Date getTime(String date) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return simpleDateFormat.parse(date);
}

From source file:Main.java

public static Date getDateFromString(String format, String time) {
    try {//from   w w  w  .j a  v a  2  s  .c om
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(time);
        return date;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Calendar parseDate(String date, String format) throws ParseException {
    Calendar lCalendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    lCalendar.setTime(formatter.parse(date));
    return lCalendar;
}

From source file:Main.java

public static Date stringToDate(String dateString) {
    try {/*  w  w  w  .  java 2 s .c o  m*/
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        return format.parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public static Date parse(String strDate, String pattern) {

    if (TextUtils.isEmpty(strDate)) {
        return null;
    }/*from   w w w .j  a  v  a  2s .  c  o  m*/
    try {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.parse(strDate);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean beforeToday(String date, String dateFormatStr) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatStr);
    return simpleDateFormat.parse(date).before(new Date());
}

From source file:Main.java

public static boolean before(String date1, String date2, String dateFormatStr) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatStr);
    return simpleDateFormat.parse(date1).before(simpleDateFormat.parse(date2));
}

From source file:Main.java

public static Date convertStringToDate(String dateIn) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;/*from   ww  w .j av  a  2 s.c o m*/
    try {
        date = formatter.parse(dateIn);
        System.out.println(date);
        System.out.println(formatter.format(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

/**
 * @param time//w ww. j a v a 2s .  co  m
 * @param format "MMM dd yyyy HH:mm:ss.SSS zzz"
 * @return
 */
public static long getEpochTime(String time, String format) {
    try {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = df.parse(time);
        return date.getTime() / 1000;
    } catch (Exception e) {
        return 0;
    }
}

From source file:Main.java

/**
 * Wrapper for SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
 * ie. "2014-05-31 19:59:08"/*from  w w w  . j  av a2 s . c om*/
 *
 * @param str "yyyy-MM-dd HH:mm:ss"
 * @return
 * @throws ParseException
 */
public static Date str2date(String str) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(str);
}