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 int compareDates(String dateTime1, String dateTime2, String format) {
    // 0 is equal, < 0 if date1 is less than date2
    try {/*w  w  w  .j a v  a2s . c o m*/
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date1 = df.parse(dateTime1);
        Date date2 = df.parse(dateTime2);
        return date1.compareTo(date2);

    } catch (ParseException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static int daysBetween(Date smdate, Date bdate) throws Exception {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    smdate = sdf.parse(sdf.format(smdate));
    bdate = sdf.parse(sdf.format(bdate));
    Calendar cal = Calendar.getInstance();
    cal.setTime(smdate);//from  w  w w. j  a v  a 2  s.  co m
    long time1 = cal.getTimeInMillis();
    cal.setTime(bdate);
    long time2 = cal.getTimeInMillis();
    long between_days = (time2 - time1) / (1000 * 3600 * 24);

    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static Date getDateFromString(int year, int month) {
    String dateString = year + "-" + (month > 9 ? month : ("0" + month)) + "-01";
    Date date = null;/*w  w w .j  a  v a  2s .  c o  m*/
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
    }
    return date;
}

From source file:Main.java

public static Date getDate(String dateStr, String format) {
    if (TextUtils.isEmpty(dateStr) || TextUtils.isEmpty(format)) {
        return null;
    }/*from   w  w  w.  ja v  a2 s .co  m*/
    Date date = null;
    try {
        SimpleDateFormat df = new SimpleDateFormat(format);
        date = df.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date getDateFromString(String strDate) {
    try {/*www .ja  v  a 2s. c  o  m*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(strDate);
        return date;
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public static String convertToReadableFormat(String input, String format) {
    try {//from  w  ww  . j av a2  s.  c om
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = df.parse(input);

        df = new SimpleDateFormat("hh:mm a");
        String readableDate = df.format(date);
        return readableDate;
    } catch (ParseException p) {
        p.printStackTrace();
    }

    return input;

}

From source file:Main.java

public static Date formatStringByFormat(String date, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {//from   w w w.  j  a  va 2  s .c  o  m
        return sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Date getDateFromString(String dateString) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
    return sdf.parse(dateString);
}

From source file:Main.java

static Date toDate(String _Input) {
    try {/* w  w  w  . j  a va  2s .c  o m*/
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(_Input);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date createDateTimeFromString(String dt_str, String dt_format) {
    if (dt_str == null || dt_format == null) {
        return null;
    }//from   w  w w  .  j  a  v a2s.  c om
    Date dt = null;
    try {
        SimpleDateFormat formatter = new SimpleDateFormat(dt_format);
        dt = formatter.parse(dt_str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dt;
}