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 final Date dateTimeFromStr(String stringDateTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date time = null;/*w  w  w.  j  a v a 2  s.co  m*/
    try {
        time = sdf.parse(stringDateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return time;
}

From source file:Main.java

public static Date StrToDate(String str) {

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    Date date = null;/*from  ww w .j a v  a2  s .  c  om*/
    try {
        date = format.parse(str);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return date;
}

From source file:de.quadrillenschule.azocamsynca.helpers.Formats.java

public static long toLong(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat(DF);
    long retval = 0;
    try {//from   ww  w .  j av  a2 s.com
        retval = sdf.parse(time).getTime() - sdf.parse("0:00:00").getTime();
    } catch (ParseException ex) {
        Logger.getLogger(Formats.class.getName()).log(Level.SEVERE, null, ex);
        return 0;
    }
    return retval;
}

From source file:Main.java

public static Date getSpritDate(String str) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");
    Date date = null;/*from  w w  w . ja v a 2s. co m*/

    try {
        date = format.parse(str);
        date.setMinutes(0);
        date.setHours(0);
        date.setSeconds(0);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return date;
}

From source file:Main.java

public static Calendar StringToCalendar(String datetime) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    try {//w  ww  .  j  a  v a  2s .co m
        calendar.setTime(simpleDateFormat.parse(datetime));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return calendar;
}

From source file:Main.java

public static Date string2Date(String s, String s1) {
    Date date = new Date();
    try {//from w  w  w  .j a va 2  s .c  o  m
        String s2 = stringNull(s1);
        SimpleDateFormat simpledateformat = new SimpleDateFormat(s2);
        String s3 = stringNull(s);
        date = simpledateformat.parse(s3);
    } catch (Exception exception) {
    }
    return date;
}

From source file:Main.java

public static int getTimestamp(String targetTime, long currentTime) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse(targetTime));
    long time1 = cal.getTimeInMillis();
    long between_days = (time1 - currentTime) / (1000 * 3600 * 24);
    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

static public Date dateFromString(String str, String format) {
    SimpleDateFormat formatter = new SimpleDateFormat(format);

    Date result = null;//  ww w .  j  a  v a  2 s .co m
    try {
        result = formatter.parse(str);
    } catch (ParseException e) {
        //e.printStackTrace();
    } catch (NullPointerException e) {
        //e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Date convertStringToDate(String dateString, String format) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.US);
    try {//from   ww  w . j  a va 2  s .  c om
        return simpleDateFormat.parse(dateString.trim());
    } catch (ParseException e) {
        Log.e(TAG, e.getMessage(), e);
    }
    return null;
}

From source file:Main.java

/**
 * Converts an XML xs:date string to a Calendar object.  We do not mess
 * around with formatting issues.  It either works, or it doesn't.
 *
 * @param xsDate the xs:date formatted string
 *
 * @return the Calendar object// ww w .j  a v  a  2s  . co  m
 *
 * @throws ParseException if a parsing error occurs
 */
public static Calendar xsDateToCalendar(final String xsDate) throws ParseException {
    Calendar newCal = null;
    if (xsDate != null) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final Date date = dateFormat.parse(xsDate);
        newCal = Calendar.getInstance();
        newCal.setTime(date);
    }
    return newCal;
}