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:bullioneconomy.bullionchart.java

public static int[] dateget(String i) throws ParseException {
    String inputDateString = i.trim().replaceAll(" ", "-");
    //System.out.println(inputDateString);
    DateFormat dfTo = new SimpleDateFormat("dd-MMM-yyyy");
    Date inputDate = dfTo.parse(inputDateString);
    DateFormat dayf = new SimpleDateFormat("dd");
    DateFormat monthf = new SimpleDateFormat("MM");
    DateFormat yearf = new SimpleDateFormat("yyyy");
    String oDay = dayf.format(inputDate);
    String oMonth = monthf.format(inputDate);
    String oYear = yearf.format(inputDate);
    int[] k = new int[3];
    k[0] = Integer.parseInt(oDay.toString());
    k[1] = Integer.parseInt(oMonth.toString());
    k[2] = Integer.parseInt(oYear.toString());

    return k;//from   www .  ja  va2s .c o  m
}

From source file:Main.java

public static Date FormatDate(String sRecDate, Context c) {
    DateFormat df = DateFormat.getDateTimeInstance();
    DateFormat dfS = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    DateFormat dfM = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    DateFormat dfL = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    DateFormat dfF = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

    DateFormat dfNoTime = DateFormat.getDateInstance();
    DateFormat dfSNoTime = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
    DateFormat dfMNoTime = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
    DateFormat dfLNoTime = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
    DateFormat dfFNoTime = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
    DateFormat dfDevice = android.text.format.DateFormat.getDateFormat(c);

    Date dRecDate = null;/*from   w ww  .ja v  a 2  s .  c o  m*/

    try {
        dRecDate = dfDevice.parse(sRecDate);
    } catch (ParseException e) {
        try {
            dRecDate = df.parse(sRecDate);
        } catch (ParseException e2) {

            try {
                dRecDate = dfS.parse(sRecDate);
            } catch (ParseException e3) {

                try {
                    dRecDate = dfM.parse(sRecDate);
                } catch (ParseException e4) {

                    try {
                        dRecDate = dfL.parse(sRecDate);
                    } catch (ParseException e5) {

                        try {
                            dRecDate = dfF.parse(sRecDate);
                        } catch (ParseException e6) {

                            try {
                                dRecDate = dfNoTime.parse(sRecDate);
                            } catch (ParseException e7) {

                                try {
                                    dRecDate = dfSNoTime.parse(sRecDate);
                                } catch (ParseException e8) {

                                    try {
                                        dRecDate = dfMNoTime.parse(sRecDate);
                                    } catch (ParseException e9) {

                                        try {
                                            dRecDate = dfLNoTime.parse(sRecDate);
                                        } catch (ParseException e10) {

                                            try {
                                                dRecDate = dfFNoTime.parse(sRecDate);
                                            } catch (ParseException e11) {

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return dRecDate;

}

From source file:edu.indiana.soic.ts.crunch.CrunchDataReader.java

public static Date getDate(String date) throws ParseException {
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    return df.parse(date);
}

From source file:io.seldon.spark.actions.JobUtils.java

public static long dateToUnixDays(String dateString) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = dateFormat.parse(dateString);

    return ((date.getTime() / 1000) / 86400);
}

From source file:com.tomdoel.mpg2dcm.EndoscopicFileProcessor.java

private static Attributes convertEndoscopicXmlToDicomAttributes(final Map<String, String> tagMap)
        throws ParseException {
    final Attributes dicomAttributes = new Attributes();
    for (final Map.Entry<String, String> entry : tagMap.entrySet()) {
        final String key = entry.getKey();
        final String value = entry.getValue();

        if (key.equals("PatID")) {
            dicomAttributes.setString(Tag.PatientID, VR.LO, value);

        } else if (key.equals("PatName")) {
            dicomAttributes.setString(Tag.PatientName, VR.PN, value);

        } else if (key.equals("ProcedureDescription")) {
            dicomAttributes.setString(Tag.StudyDescription, VR.PN, value);

        } else if (key.equals("ProcedureID")) {
            dicomAttributes.setString(Tag.StudyID, VR.PN, value);

        } else if (key.equals("ReferringPhysician")) {
            dicomAttributes.setString(Tag.ReferringPhysicianName, VR.PN, value);

        } else if (key.equals("StudyInstanceUID")) {
            dicomAttributes.setString(Tag.StudyInstanceUID, VR.UI, value);

        } else if (key.equals("SeriesInstanceUID")) {
            dicomAttributes.setString(Tag.SeriesInstanceUID, VR.UI, value);

        } else if (key.equals("OtherPatientID")) {
            dicomAttributes.setString(Tag.OtherPatientIDs, VR.UI, value);

        } else if (key.equals("PATAccession")) {
            dicomAttributes.setString(Tag.AccessionNumber, VR.SH, value);

        } else if (key.equals("PATSex")) {
            dicomAttributes.setString(Tag.PatientSex, VR.CS, value);

        } else if (key.equals("PatBirth")) {
            final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
            final Date date = dateFormat.parse(value);
            dicomAttributes.setDate(Tag.PatientBirthDate, VR.DA, date);

        } else if (key.equals("ORDate")) {
            final DateFormat orDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);
            final Date orDate = orDateFormat.parse(value);
            dicomAttributes.setDate(Tag.StudyDate, VR.DA, orDate);
            dicomAttributes.setDate(Tag.StudyTime, VR.DA, orDate);

        }/*from  w w w . j  a  v a2s . c  o m*/
    }
    return dicomAttributes;
}

From source file:com.impetus.kundera.property.accessor.DateAccessor.java

/**
 * Get Date from given below formats./*from w ww  .  j  a  v  a 2  s.  com*/
 * 
 * @param date
 *            Date pattern
 * @return date instance
 * @throws PropertyAccessException
 *             throws only if invalid format is supplied.
 */
public static Date getDateByPattern(String date) {
    if (StringUtils.isNumeric(date)) {
        return new Date(Long.parseLong(date));
    }

    for (String p : patterns) {
        try {
            DateFormat formatter = new SimpleDateFormat(p);
            Date dt = formatter.parse(date);
            return dt;
        } catch (IllegalArgumentException iae) {
            // Do nothing.
            // move to next pattern.
        } catch (ParseException e) {
            // Do nothing.
            // move to next pattern.
        }

    }
    log.error("Required Date format is not supported!" + date);
    throw new PropertyAccessException("Required Date format is not supported!" + date);
}

From source file:io.seldon.spark.actions.JobUtils.java

public static long utc_to_unixts(String utc_date) throws ParseException {
    DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date date = utcFormat.parse(utc_date);
    return (date.getTime() / 1000);
}

From source file:edu.sampleu.bookstore.document.attribs.XPathSearchableAttribute.java

/**
 * Creates an DocumentAttribute of the specified type
 */// w w  w.j  a v a2  s  . c o m
protected static DocumentAttribute createAttribute(String name, String value, String dataTypeValue)
        throws ParseException {
    if (StringUtils.isBlank(dataTypeValue)) {
        return DocumentAttributeFactory.createStringAttribute(name, value);
    } else if (KewApiConstants.SearchableAttributeConstants.DATA_TYPE_STRING.equals(dataTypeValue)) {
        return DocumentAttributeFactory.createStringAttribute(name, value);
    } else if (KewApiConstants.SearchableAttributeConstants.DATA_TYPE_DATE.equals(dataTypeValue)) {
        try {
            return DocumentAttributeFactory.createDateTimeAttribute(name,
                    CoreApiServiceLocator.getDateTimeService().convertToDate(value));
        } catch (ParseException pe) {
            // HACK: KRAD is sending us yyyy-MM-dd which is not in the standard format list...
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            Date date = dateFormat.parse(value);
            return DocumentAttributeFactory.createDateTimeAttribute(name, date);
        }
    } else if (KewApiConstants.SearchableAttributeConstants.DATA_TYPE_LONG.equals(dataTypeValue)) {
        return DocumentAttributeFactory.createIntegerAttribute(name, new BigInteger(value));
    } else if (KewApiConstants.SearchableAttributeConstants.DATA_TYPE_FLOAT.equals(dataTypeValue)) {
        return DocumentAttributeFactory.createDecimalAttribute(name, new BigDecimal(value));
    }
    throw new IllegalArgumentException("Invalid dataTypeValue was given: " + dataTypeValue);
}

From source file:mashup.fm.github.BaseService.java

/**
 * Gets the gson builder.//  ww w. j  av a  2s . co m
 * 
 * @return the gson builder
 */
private static GsonBuilder getGsonBuilder() {
    // Init Builder
    GsonBuilder builder = new GsonBuilder();

    // This is the code that will try to parse dates
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            try {
                if (arg0.getAsString().indexOf(' ') == 0) {
                    // 2011-03-23T05:14:20-07:00
                    DateFormat df = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-mm:mm");
                    return df.parse(arg0.getAsString());
                }
                DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
                return df.parse(arg0.getAsString());

            } catch (Throwable t) {
                Logger.warn(ExceptionUtil.getStackTrace(t));
                return null;
            }
        }
    });

    // We use camel case and the api returns lower case with underscore -
    // fieldId versus field_id
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    // Return Builder
    return builder;
}

From source file:com.indoqa.lang.util.TimeUtils.java

public static Date parseSolrDate(String date) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat(SOLR_DATE_FORMAT);
    dateFormat.setTimeZone(TIME_ZONE);//  w  w  w. j  a v  a  2s .  co  m
    return dateFormat.parse(date);
}