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 getCurrentAgeByBirthdate(String brithday) {

    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date date = new Date();
    java.util.Date mydate = null;
    try {/*from   w w w  .  j  av  a2s  . co m*/
        mydate = myFormatter.parse(brithday);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000) + 1;

    String year = new java.text.DecimalFormat("#").format(day / 365f);

    return year;
}

From source file:com.bootcamp.utils.DateUtils.java

public static Date getDateStart(Date date) {
    if (date == null) {
        return null;
    }/*w  w w.jav a2  s.  co  m*/
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:com.bootcamp.utils.DateUtils.java

public static Date getDateEnd(Date date) {
    if (date == null) {
        return null;
    }/* w ww  .j a  v a  2  s.c o m*/
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

/**
 * To date string.//from  w  w w  .  j a v a  2  s .  c om
 *
 * @param date the date
 * @return the date
 */
@SuppressLint("SimpleDateFormat")
public static Date toDateString(String date) {
    String formatoFecha = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    SimpleDateFormat sdf = new SimpleDateFormat(formatoFecha);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {
        return date == null ? null : sdf.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date parseTimestamp(String timestamp) {
    for (SimpleDateFormat format : ACCEPTED_TIMESTAMP_FORMATS) {
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {/*ww w . ja v  a 2 s .  c  o m*/
            return format.parse(timestamp);
        } catch (ParseException ex) {
            continue;
        }
    }

    // All attempts to parse have failed
    return null;
}

From source file:Main.java

/**
 * Parse Date by provided pattern along with target Time Zone.
 * Pattern Reference: http://developer.android.com/reference/java/text/SimpleDateFormat.html
 *
 * @param dateString String//from  w  w w  .jav a2s. c  o m
 * @param pattern String
 * @param targetTimeZone TimeZone
 * @return java.util.Date which represents based on provided dateString and pattern
 * @throws ParseException ParseException
 */
public static Date parseDate(String dateString, String pattern, TimeZone targetTimeZone) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    if (targetTimeZone != null)
        simpleDateFormat.setTimeZone(targetTimeZone);
    return simpleDateFormat.parse(dateString);
}

From source file:com.baidu.rigel.biplatform.ac.util.TimeRangeDetail.java

/**
 * ??//from  w w w. ja  v  a2s.  c o m
 * @param timeStr ?yyyyMMdd?
 * @return ??
 * @throws Exception ?
 */
public static Date getTime(String timeStr) throws Exception {
    SimpleDateFormat format = initDateFormat();
    return format.parse(timeStr);
}

From source file:Main.java

public static java.util.Calendar getCalendarFromStringDate(String dateString) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    format.setTimeZone(TimeZone.getTimeZone("ETC/UTC"));

    try {/*from w w w.  ja va2  s. c  o m*/
        Date date = format.parse(dateString);
        // System.out.println("Date ->" + date);
        calendar.setTime(date);
        return calendar;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Parses a date from a String on the format YYYY-MM-DD.
 *
 * @param dateString the String to parse.
 * @return a Date based on the given String.
 *///  ww  w  .j a  v a 2 s  .co  m
public static Date getMediumDate(String dateString) {
    try {
        final SimpleDateFormat format = new SimpleDateFormat();

        format.applyPattern(DEFAULT_DATE_FORMAT);

        return dateString != null && dateIsValid(dateString) ? format.parse(dateString) : null;
    } catch (ParseException ex) {
        throw new RuntimeException("Failed to parse medium date", ex);
    }
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

/**
 * Converts string form of date in a Date object in UTC timezone.
 *
 * @param dateInString//from  w  ww  .j  a va2 s .c  o  m
 * @return
 * @throws ParseException
 */
public static Date getDateFromString(String dateInString) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("M-dd-yyyy HH:mm");
    return sdf.parse(dateInString);
}