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 long parseFeedDate(String date) {
    SimpleDateFormat formatter;
    if (date.contains(" - ")) {
        date = date.split(" - ")[0].trim();
    }/*from  www  .ja va  2s  .c o m*/
    formatter = new SimpleDateFormat("d. MMMM yyyy");

    Date d = null;
    try {
        d = formatter.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return d.getTime();
}

From source file:com.greenline.guahao.biz.manager.partners.xm.converter.XmConverter.java

/**
 * ??//from  w  w w  .  j a v a  2 s.  c o m
 * 
 * @param yyyyMMdd yyyyMMdd?
 * @return
 */
private static long parseTimeToSecondFromYyyyMMdd(String yyyyMMdd) {
    long time = 0;
    if (null != yyyyMMdd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        try {
            date = sdf.parse(yyyyMMdd);
        } catch (ParseException e) {
            log.error(String.format("?%s", e.getMessage()), e);
        }
        time = parseTimeToSecond(date);
    }
    return time;
}

From source file:edu.utah.further.core.util.jaxb.JaxbConversionUtil.java

/**
 * Parse a string into a date.//from  w ww .j  a  v  a  2  s . c  o  m
 *
 * @param s
 *            date string. If blank, this method returns <code>null</code>
 * @param dateFormat
 *            date format
 * @return parsed date
 */
public static Date parseDateFormat(final String s, final SimpleDateFormat dateFormat) {
    if (StringUtils.isBlank(s)) {
        return null;
    }
    try {
        return dateFormat.parse(s);
    } catch (final ParseException e) {
        throw new ApplicationException("Bad date field " + s, e);
    }
}

From source file:Main.java

public static Date parseDate(String date, String[] dateParttens) throws Exception {
    SimpleDateFormat df = null;
    Date d = null;/*from  ww  w .j  a va 2  s . c om*/
    boolean isParse = false;
    for (String partten : dateParttens) {
        df = new SimpleDateFormat(partten);
        try {
            d = df.parse(date);
            isParse = true;
            break;
        } catch (ParseException e) {
            isParse = false;
        }
    }
    if (!isParse) {
        throw new Exception();
    }
    return d;
}

From source file:it.unicaradio.android.models.Transmission.java

private static String adjustTime(String time) {
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
    String adjustedTime = time;//from  ww w.ja  v  a  2  s . c om
    try {
        Date date = formatter.parse(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, 1);
        adjustedTime = formatter.format(cal.getTime());
    } catch (ParseException e) {
    }

    return adjustedTime;
}

From source file:com.b5m.you.common.util.DateUtils.java

/**
 * ?/*from   w w w . ja v a 2s .c  o m*/
 * 
 * @param deadline
 *            ,?yyyyMMddHHmmss
 * @return
 */
public static String getRemainingTime(String endDate) {
    String rtn = "";
    if (StringUtils.isBlank(endDate))
        return rtn;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// ComConstants.SDF_YYYYMMDDHHMMSS;//???
    try {
        Date deadline = sdf.parse(endDate);
        long remaining = deadline.getTime() - System.currentTimeMillis();
        // long remaining=lngDeadline-System.currentTimeMillis();
        if (remaining > 0) {
            int ms = (int) (remaining % 1000);
            remaining /= 1000;
            int sc = (int) (remaining % 60);
            remaining /= 60;
            int mn = (int) (remaining % 60);
            remaining /= 60;
            int hr = (int) (remaining % 24);
            long dy = (int) remaining / 24;
            rtn = dy + "" + hr + "?" + mn + "";// + sc + "";
        } else {
            rtn = "";
        }
    } catch (ParseException e) {

    }
    return rtn;
}

From source file:com.github.devnied.emvnfccard.utils.TrackUtils.java

/**
 * Extract track 2 data/* w  w w. j a  va 2  s . c  om*/
 * 
 * @param pEmvCard
 *            Object card representation
 * @param pData
 *            data to parse
 * @return true if the extraction succeed false otherwise
 */
public static boolean extractTrack2Data(final EmvCard pEmvCard, final byte[] pData) {
    boolean ret = false;
    byte[] track2 = TlvUtil.getValue(pData, EmvTags.TRACK_2_EQV_DATA, EmvTags.TRACK2_DATA);

    if (track2 != null) {
        String data = BytesUtils.bytesToStringNoSpace(track2);
        Matcher m = TRACK2_PATTERN.matcher(data);
        // Check pattern
        if (m.find()) {
            // read card number
            pEmvCard.setCardNumber(m.group(1));
            // Read expire date
            SimpleDateFormat sdf = new SimpleDateFormat("yyMM", Locale.getDefault());
            try {
                pEmvCard.setExpireDate(DateUtils.truncate(sdf.parse(m.group(2)), Calendar.MONTH));
            } catch (ParseException e) {
                LOGGER.error("Unparsable expire card date : {}", e.getMessage());
                return ret;
            }
            // Read service
            pEmvCard.setService(new Service(m.group(3)));
            ret = true;
        }
    }
    return ret;
}

From source file:gemlite.core.common.DateUtil.java

/**
 * yyyyMMdd?long/*from  w w w .j a v a2  s  .co  m*/
 * @param dateStr
 * @return
 */
public static long toLong(String dateStr) {
    try {
        SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyyMMdd");
        Date dt = YYYY_MM_DD.parse(dateStr);
        return dt.getTime();
    } catch (ParseException e) {
        LogUtil.getLogger().error("DateFormatUtil.toLong parser error:{}", dateStr);
    }
    return 0;
}

From source file:Main.java

public static String formatExpirationDate(String text) {

    try {/*ww w .j av  a  2 s .  com*/
        switch (text.length()) {
        case 1:
            int digit = Integer.parseInt(text);

            if (digit < 2) {
                return text;
            } else {
                return "0" + text + "/";
            }
        case 2:
            int month = Integer.parseInt(text);
            if (month > 12 || month < 1) {
                // Invalid digit
                return text.substring(0, 1);
            } else {
                return text + "/";
            }
        case 3:
            if (text.substring(2, 3).equalsIgnoreCase("/")) {
                return text;
            } else {
                text = text.substring(0, 2) + "/" + text.substring(2, 3);
            }
        case 4:
            int yearDigit = Integer.parseInt(text.substring(3, 4));
            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            int currentYearDigit = Integer.parseInt(year.substring(2, 3));
            if (yearDigit < currentYearDigit) {
                // Less than current year invalid
                return text.substring(0, 3);
            } else {
                return text;
            }
        case 5:
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yy");
            simpleDateFormat.setLenient(false);
            Date expiry = simpleDateFormat.parse(text);
            if (expiry.before(new Date())) {
                // Invalid exp date
                return text.substring(0, 4);
            } else {
                return text;
            }
        default:
            if (text.length() > 5) {
                return text.substring(0, 5);
            } else {
                return text;
            }
        }

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // If an exception is thrown we clear out the text
    return "";
}

From source file:cn.loveapple.client.android.util.DateUtil.java

/**
 * ???/*from   www.  j a v  a 2  s.c  o m*/
 * 
 * @see SimpleDateFormat ???
 * @param source ?
 * @param pattern 
 * @return ????????<code>null</code>?????????
 */
public static Date parseDate(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;
    }
}