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:eu.smartfp7.terrier.sensor.ParserUtility.java

public static EdgeNodeSnapShot parseHashMap(HashMap hashMap) throws Exception {

    /*//from  w  w w .j a  va2s.c om
     * ((HashMap)((java.util.HashMap)v.getValue()).get("crowd")).get("time");
            
    (java.lang.String) 2012-07-26T06:29:51.947Z
    (java.lang.String) #lab_visits    (java.util.ArrayList<E>) [07:00, 08:00]
            
            
    (java.util.HashMap<K,V>) {motion_spread=-1.000000, time=2012-07-26T06:29:51.947Z, motion_horizontal=-1, density=0.000000, motion_vertical=-1}
    (java.util.HashMap<K,V>) {isActive=false, temporalHint=[07:00, 08:00], name=#lab_visits, date=26-07-2012}
    (java.util.HashMap<K,V>) {motion_spread=-1.000000, time=2012-07-26T06:29:51.947Z, motion_horizontal=-1, density=0.000000, motion_vertical=-1}
    Evaluation failed. Reason(s):
    ClassCastException: Cannot cast java.util.HashMap (id=185) to java.util.Hashtable
     * */

    HashMap crowdHashMap = (HashMap) hashMap.get("crowd");
    double density = (Double) crowdHashMap.get("density");

    String time = (String) crowdHashMap.get("time");

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Calendar c = Calendar.getInstance();
    ;
    c.setTime(df.parse(time));

    double[] colors = null;

    if (crowdHashMap.containsKey("color")) {
        ArrayList colorList = (ArrayList) crowdHashMap.get("color");
        colors = new double[colorList.size()];
        for (int i = 0; i < colorList.size(); i++) {
            colors[i] = (Double) colorList.get(i);
        }
    }

    CrowdReport crowdReport = new CrowdReport(null, new Double(density), 0.0, colors);
    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(null, null, c, crowdReport);

    return snapShot;
}

From source file:com.swiftcorp.portal.common.util.CalendarUtils.java

public static Date stringToDate(String dateString) {
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    Date today = null;//from  ww  w  . j a  v a  2s .  c  om
    try {
        today = df.parse(dateString);
        // System.out.println("Today = " + df.format(today));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return today;
}

From source file:com.seajas.search.utilities.query.QueryParametersUtil.java

/**
 * Parse the given raw equivalent as the created line; this always results in two Date's, but either one of them can be null to indicate it was not specified.
 * /*from   w w  w.j  a  va  2 s  .c o m*/
 * Note that we assume that the rawEquivalent has been validated as being in the correct format.
 * 
 * @param rawEquivalent
 * @return String
 */
public static Date[] parseCreated(final String rawEquivalent) {
    String createdString = rawEquivalent.trim().replace("dcterms_created:", "");

    createdString = createdString.replace("[", "").replace("]", "").trim();

    // Now parse the date created into its two components

    String[] createdParts = createdString.split(" TO ");

    if (createdParts.length == 2) {
        DateFormat solrDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");

        Date startDate = null, endDate = null;

        try {
            if (!createdParts[0].trim().equals("NOW"))
                startDate = solrDateFormatter.parse(createdParts[0].trim());
        } catch (java.text.ParseException e) {
            logger.error("Invalid start date '" + createdParts[0] + "' - returning as 'NOW'");
        }

        try {
            if (!createdParts[1].trim().equals("NOW"))
                endDate = solrDateFormatter.parse(createdParts[1].trim());
        } catch (java.text.ParseException e) {
            logger.error("Invalid end date '" + createdParts[1] + "' - returning as 'NOW'");
        }

        return new Date[] { startDate, endDate };
    } else {
        logger.error("Invalid number of split created date arguments, taken from '" + createdString + "'");

        return new Date[] { null, null };
    }
}

From source file:eu.smartfp7.terrier.sensor.ParserUtility.java

public static EdgeNodeSnapShot parseJSON(String json) throws Exception {
    /*//from w  ww.  j ava 2 s  .  c  o  m
     * {"activity":{"name":"#lab_visits","isActive":"false","date":"26-07-2012","temporalHint":["07:00","08:00"]},"crowd":{"time":"2012-07-26T07:25:50.650Z","density":"0.000560","motion_horizontal":"-1","motion_vertical":"-1",
     * "motion_spread":"-1.000000","color":["6316128.087100","2097152.068100","4219008.049900",
     * "14729408.046900","2105376.042900","8256.041800","2113632.037800","0.037500","4210752.033700","10526944.032100",
     * "10535136.029100","8429792.028800","8224.027900","2105408.027500","4210784.026400","4202592.022200"]}}},
     * 
     * 
    */
    JsonElement jelement = new JsonParser().parse(json);

    JsonObject jobject = jelement.getAsJsonObject().getAsJsonObject("crowd");

    String time = (String) jobject.get("time").getAsString();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Calendar c = Calendar.getInstance();
    ;
    c.setTime(df.parse(time));

    String density = (String) jobject.get("density").toString();

    double[] colors = null;
    if (jobject.has("color")) {
        JsonArray jarray = jobject.getAsJsonArray("color");

        colors = new double[jarray.size()];
        for (int i = 0; i < jarray.size(); i++) {
            String v = jarray.get(0).getAsString();
            colors[i] = new Double(v);
        }

    }

    String activity = null;
    if (jelement.getAsJsonObject().has("activity")) {
        activity = jelement.getAsJsonObject().getAsJsonObject("activity").get("name").getAsString();
    }

    CrowdReport crowdReport = new CrowdReport(null, new Double(density), 0.0, colors);
    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(null, null, c, crowdReport);
    snapShot.setText((activity != null ? activity : StringUtils.EMPTY));

    return snapShot;

}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

private static Date convertDate(final Map<String, String> errors, final OfferFields field,
        final CSVRecord record) {
    final String value = record.get(field.getField());
    Date result = null;//from   w  ww .  j  a  v  a 2s .c o m

    if ((value != null) && !value.isEmpty()) {
        try {
            final DateFormat formatter = new SimpleDateFormat(DATE_FORMAT, DEFAULT_LOCALE);
            result = new Date(formatter.parse(value));
        } catch (ParseException e) {
            LOG.debug(e.getMessage(), e);
            errors.put(field.getField(), e.getMessage());
        }
    }

    return result;
}

From source file:helpers.Methods.java

/**
 * This method will return current time in hh:mm:ss format.
 *
 * @return The {@link Date} object of current time in hh:mm:ss format or
 * null if exception occurs.//from w  w w.j a v a2 s  . co m
 */
public static Date getCurrentTime() {
    Calendar currentTime = Calendar.getInstance();
    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    Date tt = null;
    try {
        tt = sdf.parse(currentTime.getTime().getHours() + ":" + currentTime.getTime().getMinutes() + ":"
                + currentTime.getTime().getSeconds());
    } catch (ParseException ex) {
        Variables.logger.Log(Methods.class, Variables.LogType.Error, "Exception in getting current time.");
    }
    return tt;
}

From source file:de.tudarmstadt.ukp.lmf.transform.germanet.InterlingualIndexConverter.java

public static MetaData getDefaultMetaData() {
    /*/*from   w ww .j  ava2s .co  m*/
     * Generate Metadata
     */
    MetaData m = new MetaData();
    m.setAutomatic(false);
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date creationDate;
    try {
        creationDate = formatter.parse("2014-04-01");
        m.setCreationDate(creationDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    m.setCreationProcess("semi-automatic");
    m.setCreationTool("http://www.sfs.uni-tuebingen.de/GermaNet/ili.shtml");
    m.setVersion("GN 9.0");
    m.setId("GNWN_ILI_0");//TODO
    return m;
}

From source file:de.knurt.fam.template.util.ContactDetailsRequestHandler.java

/**
 * return the date from a date input. input must have format ddMMyyyy (or
 * dd.MM.yyyy etc.).//from www. jav a 2  s .  c  om
 * 
 * @param birthdate
 *            as put in
 * @return the date from a date input
 */
public static Date getDate(String birthdate) {
    Date result = null;
    if (birthdate != null) {
        String input = birthdate.trim().replaceAll("[^0-9]", "");
        try {
            DateFormat df = new SimpleDateFormat("ddMMyyyy");
            df.setLenient(false);
            result = df.parse(input);
        } catch (ParseException ex) {
        }
    }
    return result;

}

From source file:Main.java

public static String ArrivalTime(long arrivalMilliseconds, String timeformat) {

    DateFormat formatter;
    Date convertedDate = null;/*w w w .j  a v a  2  s.c o m*/

    Calendar calendarToday = Calendar.getInstance();
    calendarToday.setTimeInMillis(arrivalMilliseconds);

    String Time = String
            .valueOf(calendarToday.get(Calendar.HOUR_OF_DAY) + ":" + calendarToday.get(Calendar.MINUTE));
    formatter = new SimpleDateFormat("HH:mm");
    try {
        convertedDate = (Date) formatter.parse(Time);

    } catch (ParseException e) {
        //e.printStackTrace();
    }

    if (timeformat.equals("24"))
        return formatter.format(convertedDate);
    else
        return ampmChanger(formatter.format(convertedDate));

}

From source file:net.nosleep.superanalyzer.util.Misc.java

public static Date parseXmlDate(String xml) {
    Date date = null;/*from  w  ww  .j a v a2  s .  co m*/

    try {
        // DateFormat format = new
        // SimpleDateFormat("yyyy-MM-ddTHH:mm:ss -0000");
        java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        // explicitly set timezone of input if needed
        df.setTimeZone(java.util.TimeZone.getTimeZone("Zulu"));
        date = df.parse(xml);

        // date = format.parse(xml);
    } catch (ParseException e) {
        System.out.println("Failed to parse xml date: " + xml);
    }
    return date;
}