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

/**
 * @param dateStr//from  w  w  w  . j  av a 2  s.c  o  m
 *            "yyyy-MM-dd" like
 * @return parsed Date
 * @throws ParseException
 */
public static Date parseSimple(String dateStr) {
    DateFormat dateFormat = getSimpleDateFormat();
    Date date = null;
    try {
        date = dateFormat.parse(dateStr);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static String formatTime(String date, String format, String destFormat) {
    try {/*  ww w.java  2s . c  o  m*/
        DateFormat format1 = new SimpleDateFormat(format);
        DateFormat format2 = new SimpleDateFormat(destFormat);
        return format2.format(format1.parse(date));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean isToDay(Date date) {
    DateFormat dateFormat = DateFormat.getDateInstance();
    String format = dateFormat.format(new Date());
    try {/*from  w w w  .  j ava2  s . c o  m*/
        Date today = dateFormat.parse(format);
        return today.before(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:Main.java

public static String FormatTime(String time) {
    if (time == null || time.equals("")) {
        return "";
    } else {/*from  w ww.j  a v  a2  s  .c  o m*/
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }
}

From source file:Main.java

public static String parseDateToLongForm(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//from  w  ww  .j av a 2  s  . com
    String newDate = "";
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        newDate = String.format("%1$tA, %1$tb %1$td, %1$tY", cal);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return newDate;
}

From source file:Main.java

public static float getTimeDiff(String start, String end) {
    java.text.DateFormat df = new java.text.SimpleDateFormat("HH:mm:ss");
    java.util.Date date1 = null, date2 = null;
    try {/*  ww w. j a  va2s  . c om*/
        date1 = df.parse(start);
        date2 = df.parse(end);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return (float) ((date2.getTime() - date1.getTime()));
}

From source file:Main.java

public static String formatDate(String date, String format) {
    // "ddd MM, yyyy"

    DateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
    Date parsedDate = new Date();

    try {// ww  w  .ja  va 2  s  .co m
        parsedDate = sdf.parse(date);
        parsedDate.toString();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "";
}

From source file:com.jcalvopinam.utils.Utilities.java

/**
 * Convert date from String to Date object
 *
 * @param date//from ww w  .j  a  v a 2 s  .c  o  m
 * @return
 * @throws Exception
 */
public static Date matchDate(String date) {
    try {
        if (hasFormat(date)) {
            DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
            return formatter.parse(date);
        }
    } catch (ParseException pe) {
        pe.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date ConvertStringDate(String dateString, String yyyyMMdd) {

    DateFormat formatter;
    Date convertedDate = null;//w ww  . j  ava  2  s  .  c om

    formatter = new SimpleDateFormat(yyyyMMdd);
    try {
        convertedDate = (Date) formatter.parse(dateString);
    } catch (ParseException e) {
        //e.printStackTrace();
    }

    return convertedDate;

}

From source file:io.dacopancm.oraclesp.Helper.java

/**
 *
 * @param line yyyy-MM-dd HH:mm/*from w  w w .jav a  2  s .  co  m*/
 * @return
 */
public static Date stringToDate(String line) {

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

    Date date = null;
    try {
        date = format.parse(line);
    } catch (ParseException e) {
        log.error("Lo sentimos, eso no es vlido. Por favor reintente.");
    }

    return date;
}