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 getChinaDate(String time) {
    try {/*from   w  ww  . j a  v a2  s  .  c o  m*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = format.parse(time);
        return getChinaDate(date.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getTime(String user_time, String format) {
    String re_time = null;//from   w  ww .j a  v a  2 s  .  c om
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date d;
    try {
        d = sdf.parse(user_time);
        long l = d.getTime();
        String str = String.valueOf(l);
        re_time = str.substring(0, 10);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return re_time;
}

From source file:Main.java

public static String getTimeStamp(String str) {
    long stamp = 0;
    SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {/*  w ww  .ja  va  2 s  . c om*/
        Date date = dfs.parse(str);
        stamp = date.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stamp + "";
}

From source file:Main.java

public static int getDay(String strDate, String format) {
    int day = 0;/*  w  w  w  . ja v  a 2s. co  m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        day = c.get(Calendar.DAY_OF_MONTH);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return day;
}

From source file:Main.java

public static long getTimeMilliSec(String timeStamp) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {/*w w  w  .  j a  va2 s. com*/
        Date date = format.parse(timeStamp);
        return date.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static Date convertString2Date(String source) {

    if (null == source || source.equals("")) {
        return null;
    }/*from   w w w .  j a v  a  2s.c  o m*/

    SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
    try {
        return format.parse(source);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static int getMonth(String strDate, String format) {
    int month = 0;
    try {/*from  w  w w.  j  a  va  2  s . c o  m*/
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        month = c.get(Calendar.MONTH) + 1;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return month;
}

From source file:Main.java

public static Date convertStringToDate(String date) {
    Date result = null;/*from  w w w .  j av  a2s. co  m*/
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {
        result = format.parse(date);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Date str2Date(String str, String format) {
    if (str == null || str.length() == 0) {
        return null;
    }//from w  ww. ja v a 2  s  . c  o  m
    if (format == null || format.length() == 0) {
        format = FORMAT;
    }
    Date date = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(str);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return date;

}

From source file:Main.java

/**
 * time: 2016/10/19 11:42/*from   w  ww.j  a v a  2s . c om*/
 * description:
 */
public static Date getData(String time) {
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date parse = myFormatter.parse(time);
        return parse;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}