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 Date getFormatDate(int year, int month, int day) {
    String strDate = year + "-" + month + "-" + day;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try {//  w w  w . j a  v a 2 s  .  c  o m
        return format.parse(strDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** 
 * Returns a java.util.Date for a given xml xs:dateTime, ignoring the timezone. Needs to be
 * updated to include the numerous xs:dateTime year, timezone, etc. specifications.
 * //from ww w. j a v  a 2 s . c o  m
 * @param xmlDate Date string formatted as xs:dateTime
 * @return java.util.Date for given xmlDate
 * @throws ParseException
 */
public static Date parseXMLDate(String xmlDate) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    return formatter.parse(xmlDate);
}

From source file:Main.java

public static Date parseDate(String date, String pattern) {
    Date result = null;/*  w w  w. j a  va  2 s . c  o m*/
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    try {
        result = dateFormat.parse(date);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Date dateJSONStringToDateCorreu(String sDate) {
    Date date = null;/*from  w  ww .j  a  v a 2s . c  o  m*/
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("\"dd/MM/yyyy HH:mm\"");
        date = formatter.parse(sDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date stringToHour(String str) {
    Date date = null;/*from   ww w  . j  a  v  a 2 s  .  c o m*/
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    try {
        date = formatter.parse(str);
        return date;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String formatDateFromString(String input) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {/*from   w w w  .  ja  va2 s.c o  m*/
        date = sdf.parse(input);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    sdf = new SimpleDateFormat("dd/MM/yyyy");
    String result = sdf.format(date);
    return result;
}

From source file:Main.java

public static boolean checkDate(String str) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {/* w  w  w.j av  a2 s.c o m*/
        formatter.parse(str);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * String to date. e.g. 2013-12-13//from  w  w  w .  j  ava2  s.  c  o m
 * 
 * @param dateStr
 * @return
 */
public static Date str2Date(String dateStr) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(sdf.parse(dateStr).getTime());
        sdf = null;
        return date;
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public static Date formatFromString(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {//from w w w.j  a  v a  2s.c  om
        return sdf.parse(date);
    } catch (ParseException e) {
        //         e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date formatStringToDate(String str) {
    Date date = null;//from   w w w.  j  a va 2 s .c o  m
    try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = simpleDateFormat.parse(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return date;
}