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 StringToDate(String dateStr) {
    SimpleDateFormat dd = new SimpleDateFormat(fm5);
    Date date = null;//from   w ww.  j  a va 2  s .  c o  m
    try {
        date = dd.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date getDateFromFormattedTimeStamp(String formattedTimeStamp) throws ParseException {
    SimpleDateFormat date = new SimpleDateFormat(MARTUS_SIGNATURE_FILE_DATE_FORMAT);
    return date.parse(formattedTimeStamp);
}

From source file:Main.java

public static long formatToLong(String time, String template) {
    SimpleDateFormat sdf = new SimpleDateFormat(template, Locale.CHINA);
    try {/*from  www  .  j a v a  2  s  .c  o  m*/
        Date d = sdf.parse(time);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        long l = c.getTimeInMillis();
        return l;
    } catch (ParseException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static Date ConvertToDateTime(String format, String dateTimeString) {
    if (dateTimeString.length() == 0) {
        return null;
    }/*from   w  w  w  . j  av a2 s  .c o m*/
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date;
    try {
        date = sdf.parse(dateTimeString);
    } catch (Exception ex) {
        date = null;
    }
    return date;
}

From source file:Main.java

public static String ymd2Timestamp(String dateString) throws ParseException {
    String timeStamp = null;/*from  w  w  w.j ava  2s.co m*/
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    Date d;
    try {
        d = sdf.parse(dateString);
        long l = d.getTime() / 1000;
        timeStamp = String.valueOf(l);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return timeStamp;
}

From source file:Main.java

/**
 * time1 > time2 ->return >1; time1=time2 >0;time1<time2 >-1
 * @param time1//from  w ww . ja va  2s.c  o m
 * @param time2
 * @return
 */
public static int compareTime(String time1, String time2) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(time1);
        Date date2 = sdf.parse(time2);
        long ret = (date1.getTime() - date2.getTime());
        if (ret > 0)
            return 1;
        else if (ret < 0)
            return -1;
        else
            return 0;

    } catch (Exception e) {
    }
    return 0;
}

From source file:Main.java

public static Date StringToDate(String str) {
    SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
    Date d = null;/*from  w w  w  . j  av a  2  s  .c om*/
    try {
        d = sim.parse(str);
    } catch (ParseException e) {
        System.out.println(e.getMessage().toString());
    }
    return d;
}

From source file:Main.java

public static Calendar getDateFromString(String strDate) {
    Calendar date = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    try {/*from   w ww .j a  va 2 s  .c o m*/
        date.setTime(simpleDateFormat.parse(strDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static String formatTime_YYYYMMDD(String time) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {/*from  w w  w . j  a va2s .co  m*/
        return formatter.format(formatter.parse(time));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static Date dateXMLStringToDateParser(String sDate) {
    Date date = null;/*  w w  w. j av a2 s .  c om*/
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
        date = formatter.parse(sDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}