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:com.espertech.esper.client.util.DateTime.java

private static Date parse(String str, SimpleDateFormat format) {
    Date d;/*from   ww w .  jav a 2 s . c  o  m*/
    try {
        d = format.parse(str);
    } catch (ParseException e) {
        log.warn("Error parsing date '" + str + "' according to format '" + format.toPattern() + "': "
                + e.getMessage(), e);
        return null;
    }
    return d;
}

From source file:com.wrapp.android.webimage.ImageDownloader.java

public static Date getServerTimestamp(final URL imageUrl) {
    Date expirationDate = new Date();

    try {/*from ww w.  j  av  a2  s .c o  m*/
        final String imageUrlString = imageUrl.toString();
        if (imageUrlString == null || imageUrlString.length() == 0) {
            throw new Exception("Passed empty URL");
        }
        LogWrapper.logMessage("Requesting image '" + imageUrlString + "'");
        final HttpClient httpClient = new DefaultHttpClient();
        final HttpParams httpParams = httpClient.getParams();
        httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, CONNECTION_TIMEOUT_IN_MS);
        httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT_IN_MS);
        httpParams.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
        final HttpHead httpHead = new HttpHead(imageUrlString);
        final HttpResponse response = httpClient.execute(httpHead);

        Header[] header = response.getHeaders("Expires");
        if (header != null && header.length > 0) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
            expirationDate = dateFormat.parse(header[0].getValue());
            LogWrapper
                    .logMessage("Image at " + imageUrl.toString() + " expires on " + expirationDate.toString());
        }
    } catch (Exception e) {
        LogWrapper.logException(e);
    }

    return expirationDate;
}

From source file:com.hagreve.android.lib.HaGreveApi.java

/**
 * Obtains the list of current strikes./*from w w  w  .  ja v  a 2  s .  com*/
 * @return Array of Strike objects
 */
public static Strike[] getStrikes() {
    Strike[] items = new Strike[0];

    String result = doGetRequest(BASE_URL + "/strikes");

    if (result != null) {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    return formatter.parse(json.getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage());
                }
            }

        });
        Gson gson = builder.create();
        items = gson.fromJson(result, Strike[].class);
    }

    return items;
}

From source file:dev.meng.wikipedia.profiler.util.StringUtils.java

public static GregorianCalendar parseTimestamp(String string, String format) throws ParseException {
    GregorianCalendar result = null;

    SimpleDateFormat formatter = new SimpleDateFormat(format);
    result = (GregorianCalendar) GregorianCalendar.getInstance();
    result.setTime(formatter.parse(string));

    return result;
}

From source file:Main.java

public static String findDateWithFormat(String findDate, String format) {
    findDate = findDate.replace(" ", "").replace("-", "").replace(":", "").replace("/", "").replace(".", "");

    Locale currentLocale = new Locale("KOREAN", "KOREA");
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", currentLocale);
    SimpleDateFormat returnFormatter = new SimpleDateFormat(format, currentLocale);
    String returnValue = "";
    try {//from  w w w  .j a  v a2s. c om
        Date fDate = formatter.parse(findDate);
        returnValue = returnFormatter.format(fDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return returnValue;
}

From source file:gob.dp.simco.comun.FunctionUtil.java

public static Date formatearDate(Date fecha, String pattern) {
    SimpleDateFormat formato = new SimpleDateFormat(pattern);
    Date fechaFormateada = null;/*  ww w.  ja  va 2 s.  c  o  m*/
    try {
        fechaFormateada = formato.parse(formato.format(fecha));
        return fechaFormateada;
    } catch (Exception ex) {
        return fechaFormateada;
    }
}

From source file:com.itd.dbmrgdao.TestTime3.java

protected static String CompareTime(String a, String b, String operator) throws ParseException {

    if (a == null) {
        return b;
    }/*www . j a  v a 2s .c o  m*/

    SimpleDateFormat parser1 = new SimpleDateFormat("HH:mm");
    Date x = parser1.parse(a);
    Date y = parser1.parse(b);

    try {
        // The Magic happens here i only get the Time out of the Date Object
        SimpleDateFormat parser2 = new SimpleDateFormat("HH:mm");
        x = parser2.parse(parser2.format(x));
        y = parser2.parse(parser2.format(y));

    } catch (ParseException ex) {
        System.err.println(ex);

    }

    switch (operator) {
    case "01":
        return ((x.compareTo(y) <= 0) ? a : b);
    case "04":
        return ((x.compareTo(y) >= 0) ? a : b);
    default:
        throw new IllegalArgumentException("Operator " + operator + " not fould in control!");
    }
}

From source file:Main.java

public static String convertUtc2LocalPro(String utcTime) {
    if (TextUtils.isEmpty(utcTime)) {
        return "";
    }//  w w  w  .j  av  a 2 s .  c  om
    // 2014-10-24T02:58:05.932Z
    SimpleDateFormat utcFormatter, localFormatter;
    utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
    Date date;
    Calendar cal = Calendar.getInstance();
    try {
        date = utcFormatter.parse(utcTime);
        cal.setTime(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.add(Calendar.HOUR_OF_DAY, +8);
    date = cal.getTime();
    localFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    return localFormatter.format(date);
}

From source file:Main.java

public static String string2Timezone(String srcFormater, String srcDateTime, String dstFormater,
        String dstTimeZoneId) {//  ww  w.  ja  v  a  2  s  . c o m
    if (srcFormater == null || "".equals(srcFormater))
        return null;
    if (srcDateTime == null || "".equals(srcDateTime))
        return null;
    if (dstFormater == null || "".equals(dstFormater))
        return null;
    if (dstTimeZoneId == null || "".equals(dstTimeZoneId))
        return null;
    SimpleDateFormat sdf = new SimpleDateFormat(srcFormater);
    try {
        int diffTime = getDiffTimeZoneRawOffset(dstTimeZoneId);
        Date d = sdf.parse(srcDateTime);
        long nowTime = d.getTime();
        long newNowTime = nowTime - diffTime;
        d = new Date(newNowTime);
        return date2String(dstFormater, d);
    } catch (ParseException e) {
        return null;
    } finally {
        sdf = null;
    }
}

From source file:Main.java

public static Date string2Date(String s, String style) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    simpleDateFormat.applyPattern(style);
    Date date = null;/* w w w.j  a va 2 s .  c  om*/
    if (s == null || s.length() < 6) {
        return null;
    }
    try {
        date = simpleDateFormat.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}