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 Date StringToDate(String dateStr, String formatStr) {
    DateFormat format = new SimpleDateFormat(formatStr);
    Date date = null;//from   w ww .  j  a va  2  s  .  co m
    try {
        date = format.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static boolean isDateValid(int day, int month, int year) {
    try {//from www .  j  a v  a 2s.  c o  m
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        df.setLenient(false);
        df.parse(day + "-" + month + "-" + year);
        return true;
    } catch (ParseException e) {
        return false;
    }
}

From source file:Util.java

public static Timestamp convertStringToTimestamp(String str_date) {
    try {//  www . j  a v a 2s . c o  m
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date date = (Date) formatter.parse(str_date);
        java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());

        return timeStampDate;
    } catch (ParseException e) {
        System.out.println("Exception :" + e);
        return null;
    }
}

From source file:Main.java

public static Date dateFormatter(String dateString) {
    DateFormat formatter = new SimpleDateFormat("DD-MM-yyyy");
    Date date;//w  w w . j a v a 2 s.c om
    try {
        date = (Date) formatter.parse(dateString);
        return date;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date stringToDate(String dateStr, String formatStr) {
    DateFormat sdf = new SimpleDateFormat(formatStr);
    Date date = null;/*from  ww  w.  j  a va2 s .  c o  m*/
    try {
        date = sdf.parse(dateStr);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

/**
 * Get week by datetime, datetime format must be contracted with DATE_FORMAT.
 *
 * @param datetime/*from www.  j a v  a  2s  .  c o  m*/
 * @return
 */
public static String getWeek(String datetime) {
    Calendar cal = Calendar.getInstance();
    DateFormat f = new SimpleDateFormat(DATE_FORMAT);
    try {
        cal.setTime(f.parse(datetime));
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return WEEKS[week_index];
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static Date strToDate(String dateStr) {
    Date date = null;//  w w  w .j  a va2s . c o  m

    if (dateStr != null && (!dateStr.equals(""))) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return date;
}

From source file:Main.java

/**
 * Permet de convertir une date du format annee-mois-jour 
 * en une instance de la classe java.util.Date
 * /*w ww  .  ja  v  a 2  s  .  c  o m*/
 * @param stringDateFormat
 * @return
 */
public static Date toDate(String stringDateFormat) {
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date convertedDate = (Date) formatter.parse(stringDateFormat);
        return convertedDate;
    } catch (ParseException e) {

        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static void setHttpResponseDate(String date) {
    try {//from   ww  w.  j av  a2  s  .  co  m
        DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);

        Date d = fmt.parse(date.trim());

        if (d.getTime() < RELEASE_ZERO_TIMESTAMP)
            return;

        TIME_CALIBRATOR = d.getTime() - System.currentTimeMillis();

    } catch (Exception e) {
        // just ignore
    } catch (Error error) {
        // just ignore
    }
}

From source file:Main.java

public static Date readDate(Element e, String attributeName, DateFormat format) throws ParseException {
    String dateStr = readAttributeValue(e, attributeName);
    return format.parse(dateStr);
}