Example usage for java.text DateFormat parse

List of usage examples for java.text DateFormat parse

Introduction

In this page you can find the example usage for java.text DateFormat 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 getAge(String fec_nac) {

    DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    Calendar dob = Calendar.getInstance();

    Date d = null;//ww w.  j a va  2  s .  co m
    try {
        d = df.parse(fec_nac);

    } catch (Exception e) {
    }

    Calendar today = Calendar.getInstance();
    dob.setTime(d);

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
        age--;
    }
    return String.valueOf(age);
}

From source file:Main.java

public static String FormatTime(String Time, String timeformat) {

    DateFormat formatter;
    Date convertedDate = null;/*from  w ww . jav  a  2 s.  co m*/

    formatter = new SimpleDateFormat("HH:mm");
    try {
        convertedDate = (Date) formatter.parse(Time);

    } catch (ParseException e) {
        //e.printStackTrace();
    }

    if (timeformat.equals("24"))
        return formatter.format(convertedDate);
    else
        return ampmChanger(formatter.format(convertedDate));

}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

private static Date parse(String dateString, DateFormat dateFormat) {
    try {//from w  w  w . j av  a  2  s .  co  m
        return dateFormat.parse(dateString);
    } catch (ParseException e) {
        log.error("ParseException: " + e.getMessage());
    }
    return null;
}

From source file:Main.java

private static DateTime getDateFromISO(String ISOString) {
    //DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
    try {// ww  w  .  ja v  a2 s  .  c o  m
        return parser.parseDateTime(ISOString);
    } catch (Throwable e) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
            df.setTimeZone(TimeZone.getTimeZone("UTC"));
            return new DateTime(df.parse(ISOString));
        } catch (Throwable ignored) {
            Log.d("ERROR", "Failed parsing string into date");
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static Date readDateIfExists(Element e, String attributeName, DateFormat format) throws ParseException {
    String dateStr = readAttributeValue(e, attributeName, null);

    if (dateStr == null) {
        return null;
    }/*  ww w  .  j a va  2  s.  c om*/

    return format.parse(dateStr);
}

From source file:Main.java

public static String getTimeFormatNO1(String time) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    DateFormat wantdf = new SimpleDateFormat("yyyy-MM-dd");
    try {/*from   w ww . j av  a  2 s  . co  m*/
        Date current = new Date();
        Date tiemBefore = df.parse(time.substring(0, 19).replace("T", " "));
        String str = wantdf.format(tiemBefore);
        return str;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "null";
}

From source file:Main.java

public static String readableDate(String dateString) {
    DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ");
    DateFormat output = new SimpleDateFormat("dd.MM.yyyy HH:mm");

    Date date = new Date();
    try {//  w  w w .j a va 2s .c  o  m
        date = input.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return output.format(date.getTime());
}

From source file:Main.java

/**
 * Method to check difference between actual time and saved time in xml file.
 * //w  w w. j a v a 2 s.c  om
 * @param target document
 * @return time difference as integer
 * @throws ParseException
 */
public static int checkDateDifference(Document target) throws ParseException {
    int diff = 0;
    NodeList parents = target.getElementsByTagName("g:options");
    Element parent = (Element) parents.item(0); //g:options - only 1 element
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
    //date xml
    String time = parent.getAttribute("time");
    Date oldT = format.parse(time);
    //date now
    Date newT = new Date();
    //compare
    diff = (int) ((newT.getTime() - oldT.getTime()) / 1000);
    return diff;
}

From source file:com.taobao.itest.util.DateConverter.java

public static Date strToDate(String str, String pattern) throws ParseException {
    if (pattern == null) {
        pattern = "yyyy-MM-dd HH:mm:ss.SSS";
    }//from   w  ww.j  ava2 s. c om
    DateFormat ymdhmsFormat = new SimpleDateFormat(pattern);
    return ymdhmsFormat.parse(str);
}

From source file:com.baifendian.swordfish.common.utils.DateUtils.java

/**
 * ?/*  w w  w  . ja  v  a2 s.  co m*/
 *
 * @param dateStr
 * @param formatString
 * @return
 */
public static Date parse(String dateStr, String formatString) {
    try {
        DateFormat formatter = new SimpleDateFormat(formatString);

        return formatter.parse(dateStr);
    } catch (ParseException e) {
        throw new RuntimeException("Time parse failed exception", e);
    }
}