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 String getUTCTimeFromLocal(String localTime) {
    java.util.Date UTCDate = null;
    String localTimeStr = null;/*from  w w w . j  a  va 2s. c  o m*/
    SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
    try {
        UTCDate = format.parse(localTime);
        localTimeStr = format.format(UTCDate.getTime() - TimeZone.getDefault().getRawOffset());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return localTimeStr;
}

From source file:Main.java

public static String getLocalTimeFromUTC(String UTCTime) {
    java.util.Date UTCDate = null;
    String localTimeStr = null;/*from w w w.  j  a v a 2 s  .  com*/
    SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
    try {
        UTCDate = format.parse(UTCTime);
        localTimeStr = format.format(UTCDate.getTime() + TimeZone.getDefault().getRawOffset());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return localTimeStr;
}

From source file:Main.java

public static Date stringToDate(String strTime, String formatType) {
    SimpleDateFormat formatter = new SimpleDateFormat(formatType);
    Date date = null;/*from w  ww .  j  a  va 2s  .  com*/
    try {
        date = formatter.parse(strTime);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date parseToDate(String dateString, String pattern) {

    if (pattern == null || "".equals(pattern)) {
        pattern = "yyyy-MM-dd HH:mm:ss";
    }/*from  w ww. ja v a 2 s  . c o m*/

    SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
    try {
        return formatter.parse(dateString);
    } catch (ParseException e) {

    }
    return new Date();
}

From source file:Main.java

public static long parseTimeStamp(String timeStamp) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    Date date;//from   ww  w  . j a  va 2  s.  co  m
    try {
        date = format.parse(timeStamp);
    } catch (ParseException e) {
        SimpleDateFormat optFormat = new SimpleDateFormat("yy-MM-dd HH:mm");
        date = optFormat.parse(timeStamp);
    }
    return date.getTime();
}

From source file:Main.java

public static Date date(String date) {
    SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
    Date value = null;// ww  w.  j ava2s .  c o m
    try {
        value = format.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static Date stringToDate(String str, String format, Locale locale) {
    Date date = null;/*  www . j ava  2  s  .c o  m*/
    SimpleDateFormat formatter = new SimpleDateFormat(format, locale);
    try {
        date = formatter.parse(str);
        return date;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Date strToTime(String str) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;/*from www.  ja  va  2s  .  c o  m*/
    try {
        date = format.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return date;

}

From source file:Main.java

public static Date convertStringToDate(String date) {
    //TODO Set this date in a constant file
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    try {/*w w  w .jav a 2  s .c  om*/
        return format.parse(date);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Calendar YYYYMMDDHHMMSSToCalendar(String str) {
    if (str != null) {
        Calendar cal = Calendar.getInstance();
        try {//from  w  w  w .ja v  a 2  s . com
            SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            cal.setTime(FORMAT.parse(str));
            return cal;
        } catch (ParseException e) {
            return null;
        }
    }
    return null;
}