List of usage examples for java.text SimpleDateFormat parse
public Date parse(String source) throws ParseException
From source file:Main.java
public static Calendar getFinishDate(String startDate, String duration) { try {/* www .j ava2 s. c o m*/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Calendar calendarFinish = Calendar.getInstance(); calendarFinish.setTime(sdf.parse(startDate)); calendarFinish.add(Calendar.DATE, Integer.parseInt(duration)); return calendarFinish; } catch (ParseException e) { Log.e("PARSE_FAIL", Log.getStackTraceString(e)); } return null; }
From source file:Main.java
public static long getLongDate(String date) { try {// ww w .j a v a2 s .com SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault()); sdf.setTimeZone(TimeZone.getDefault()); Date d = sdf.parse(date); return d.getTime(); } catch (Exception e) { e.printStackTrace(); } return 0; }
From source file:Main.java
/** * This is a helper method for checking if the fromDate is later than the * toDate. This is necessary in case a user sends the dates with HTTP GET. * * @param fromDate/*from w w w . java 2s .co m*/ * @param toDate * @return boolean */ public static boolean checkDates(String fromDate, String toDate) { String formatString = DEFAULT_DATE_FORMAT; SimpleDateFormat sdf = new SimpleDateFormat(formatString); Date date1 = null; Date date2 = null; try { date1 = sdf.parse(fromDate); date2 = sdf.parse(toDate); } catch (ParseException e) { return false; // The user hasn't specified any dates } if (!date1.before(date2)) { return true; // Return true if date2 is earlier than date1 } else { return false; } }
From source file:Main.java
public static Date parseISO8601Date(String date) { if (date.length() > ISO8601_SHORT.length()) { return parseRFC3339Date(date); }//w w w . j a va 2s . c om Date result = null; if (date.length() == "YYYYMMDD".length()) { date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8); } SimpleDateFormat format = ISO8601ShortFormatter.get(); try { result = format.parse(date); } catch (ParseException e) { e.printStackTrace(); } return result; }
From source file:Main.java
static Date ConvertStringToDate(String dateString) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault()); Date convertedDate = new Date(); try {//from w w w.j av a2 s . c om convertedDate = dateFormat.parse(dateString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return convertedDate; }
From source file:Main.java
/** * <p>//from w w w .j a v a2 s. com * <b> public int compare(mDate, dateToCOmpareWith, format) </b> * </p> * * Compares two dates with each other using the rules of the given date * format. * * @param mDate * The date you want to compare * @param dateToCompareWith * The date comparing with * @param format * An accepted format which must be valid for a * <code>SimpleDateFormat</code> * @return <p> * returns 0 if the dates are equal * </p> * <p> * returns > 0 if mDate is larger then the date to compare with * </p> * <p> * returns < 0 if mDate is smaller then the date to compare with * </p> */ public static int compareDates(String mDate, String dateToCompareWith, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); Date date1 = null; Date date2 = null; try { date1 = sdf.parse(mDate); date2 = sdf.parse(dateToCompareWith); } catch (ParseException e) { e.printStackTrace(); } return date1.compareTo(date2); }
From source file:Main.java
/** * Parse the serialized string form into a java.util.Date * * @param date// w w w . j a v a 2 s. c om * The serialized string form of the date * @return The created java.util.Date */ public static long parseAtomDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat(); for (String s : masksAtom) { try { sdf.applyPattern(s); return sdf.parse(date).getTime(); } catch (Exception e) { } } return 0; }
From source file:Main.java
public static Date strToDate(String dateString) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzzZ yyyy", Locale.ENGLISH); Date date = null;//from w ww. j a v a 2 s. c om try { date = simpleDateFormat.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return date; }
From source file:Main.java
/** * parse a string to an Object of given data type. <br> * @param strValue the string value//from www .j a v a 2 s . co m * @param valueType the full java class name which could be <br> * java.util.Date (default format is yyyy-MM-dd) <br> * java.sql.Date (default format is yyyy-MM-dd) <br> * java.sql.Timestamp (default format is yyyy-MM-dd HH:mm:ss) <br> * java.sql.Time (default format is HH:mm:ss) <br> * java.lang.String <br> * java.lang.Boolean <br> * java.lang.Double <br> * java.lang.Long <br> * java.lang.Short <br> * java.lang.Integer <br> * java.lang.Byte <br> * java.lang.Float <br> * java.math.BigInteger <br> * java.math.BigDecimal <br> * * @param format SimpleDateFormat allowed standard formats. * @return Object * @throws Exception */ public static Object parseStringToObject(String strValue, String valueType, String format) throws Exception { if ("java.util.Date".equals(valueType)) { // default format yyyy-MM-dd SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd"); return (fmt.parse(strValue)); } else if ("java.sql.Date".equals(valueType)) { // default format yyyy-MM-dd SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd"); return (new java.sql.Date(fmt.parse(strValue).getTime())); } else if ("java.sql.Timestamp".equals(valueType)) { // default format yyyy-MM-dd HH:mm:ss SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "yyyy-MM-dd HH:mm:ss"); return (new java.sql.Timestamp(fmt.parse(strValue).getTime())); } else if ("java.sql.Time".equals(valueType)) { // default format HH:mm:ss SimpleDateFormat fmt = new SimpleDateFormat(format != null ? format : "HH:mm:ss"); return (new java.sql.Time(fmt.parse(strValue).getTime())); } else if ("java.lang.Boolean".equals(valueType)) { return (Boolean.valueOf(strValue)); } else if ("java.lang.Double".equals(valueType)) { return (Double.valueOf(strValue)); } else if ("java.lang.Long".equals(valueType)) { return (Long.valueOf(strValue)); } else if ("java.lang.Short".equals(valueType)) { return (Short.valueOf(strValue)); } else if ("java.lang.Integer".equals(valueType)) { return (Integer.valueOf(strValue)); } else if ("java.lang.Byte".equals(valueType)) { return (Byte.valueOf(strValue)); } else if ("java.lang.Float".equals(valueType)) { return (Float.valueOf(strValue)); } else if ("java.math.BigInteger".equals(valueType)) { return (new java.math.BigInteger(strValue)); } else if ("java.math.BigDecimal".equals(valueType)) { return (new java.math.BigDecimal(strValue)); } else { return (strValue); } }
From source file:cn.loveapple.client.android.utils.DateUtil.java
/** * ???//from w ww . j a v a 2 s . c o m * * @see SimpleDateFormat ??? * @param source ? * @param pattern * @return ????????<code>null</code>????????? */ public static Date paseDate(String source, String pattern) { if (source == null || pattern == null) { return null; } try { SimpleDateFormat format = new SimpleDateFormat(pattern); return format.parse(source); } catch (Exception e) { return null; } }