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

/**
 * Parses XML datetime object to Date object
 *
 * @param xmlDateTime XML Datetime as a String
 * @return Datetime object//from  ww w.  jav  a 2s.  c  om
 * @throws ParseException if Parsing exception occurred
 */
public static Date parseXmlDateTime(String xmlDateTime) {
    Date parsedDate = new Date();
    try {
        xmlDateTime = xmlDateTime.replace("Z", "+0000");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
        parsedDate = dateFormat.parse(xmlDateTime);
        return parsedDate;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return parsedDate;
}

From source file:Main.java

public static boolean validTime(String str, String pattern) {
    DateFormat formatter = new SimpleDateFormat(pattern, Locale.ENGLISH);
    Date date = null;/* ww w . j a  va  2  s  . co  m*/
    try {
        date = (Date) formatter.parse(str);
    } catch (ParseException e) {
        return false;
    }
    return str.equals(formatter.format(date));
}

From source file:Main.java

public static Date getDate(String stringDate) {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date date = null;//  w  w  w  . j a v  a2  s .  com
    try {
        date = format.parse(stringDate);
    } catch (ParseException e) {
    }
    return date;
}

From source file:Main.java

public static int getDayFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//  www .  j a va  2 s .c  o  m
    int day = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        day = cal.get(Calendar.DAY_OF_MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return day;
}

From source file:Main.java

public static Date tryParseDate(String dateStr, Date defaultValue) {
    if (dateStr == null) {
        return defaultValue;
    }//from  ww  w .  jav a  2s.  c om
    DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = defaultValue;
    try {
        date = dateformat.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static int getYearFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//from   ww  w .  ja  v  a 2  s  .  c o  m
    int year = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        year = cal.get(Calendar.YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return year;
}

From source file:Main.java

public static int getMonthFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;/*  w  w w  .ja v  a2s.  co  m*/
    int month = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        month = cal.get(Calendar.MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return month;
}

From source file:Main.java

public static Date convertDateTimeToDate(String dateTime) {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.ENGLISH);
    Date date = null;// w  w w.  j a  va 2 s .co m
    try {
        date = format.parse(dateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    //        System.out.println(date);
    return date;
}

From source file:Main.java

public static int[] parseDate(String dateString) {
    try {//w w  w .  j a va  2  s  .  com
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setLenient(false);
        Date date = (Date) formatter.parse(dateString);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int[] dateInts = new int[3];
        dateInts[0] = cal.get(Calendar.DAY_OF_MONTH);
        dateInts[1] = cal.get(Calendar.MONTH);
        dateInts[2] = cal.get(Calendar.YEAR);

        return dateInts;
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not parse date:'" + dateString + "'. Date format is dd-MM-yyyy (ex: 31-12-2011).", e);
    }
}

From source file:Main.java

/**
 * @param dateStr/* www  .  j a  va2s. co m*/
 *            "yyyy-MM-dd HH:mm:ss" like
 * @return parsed Date
 * @throws ParseException
 */
public static Date parseDetail(String dateStr) {
    DateFormat dateFormat = getDetailDateFormat();
    Date date = null;
    try {
        date = dateFormat.parse(dateStr);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}