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.esofthead.mycollab.core.utils.DateTimeUtils.java

/**
 * /*from  ww w.ja  v  a 2  s  .c  o  m*/
 * @param strDate
 * @return
 */
public static Date convertDateByFormatW3C(String strDate) {
    String formatW3C = "yyyy-MM-dd'T'HH:mm:ss";
    if (strDate != null && !strDate.equals("")) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatW3C);
        try {
            return formatter.parse(strDate);
        } catch (ParseException e) {
            LOG.error("Error while parse date", e);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Convert format date//from   w ww  .j  a  v  a2 s  .  co m
 * @param String date
 * @param String from_format
 * @param String to_format
 * @return
 */
public static String convertFormatDate(String date, String from_format, String to_format) {
    SimpleDateFormat inputFormat = null;
    SimpleDateFormat outputFormat = null;
    Date formattedDate = null;

    try {
        inputFormat = new SimpleDateFormat(from_format);
        outputFormat = new SimpleDateFormat(to_format);
        formattedDate = inputFormat.parse(date);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }

    return outputFormat.format(formattedDate);
}

From source file:com.siphyc.utils.Utilities.java

public static Date getTimePart(String timeString) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    return sdf.parse(timeString);
}

From source file:com.traffitruck.web.JsonController.java

public static Date convertDriveDate(String drivedate) {
    Date driveDateObj = null;//  ww  w . j  a  v a2  s.  c  om
    if (drivedate != null && drivedate.length() > 0) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {
            driveDateObj = sdf.parse(drivedate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    return driveDateObj;
}

From source file:com.beginner.core.utils.DateUtil.java

/**
 * yyyy-MM-dd HH:mm:ss?/*from w  w  w  . j  a  v  a2 s  .co  m*/
 * @param date    
 * @return       Date
 * @since       1.0.0
 */
public static Date str2Date(String date) {
    if (Tools.isNotEmpty(date)) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    } else {
        return null;
    }
}

From source file:com.siphyc.utils.Utilities.java

public static Date getDatePart(String dateString) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.parse(dateString);
}

From source file:io.github.sparta.helpers.date.DateUtil.java

/**
 * Convert string date to Date with {@value #SIMPLE_DATE_FORMAT}.
 * /* w  w w.  jav a2 s .  co  m*/
 * @param strDate
 *            date string
 * @return date
 * @throws java.text.ParseException
 *             occurs when given steDate is not {@link #SIMPLE_DATE_FORMAT}
 */
public static Date toSimpleDate(String strDate) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    return simpleDateFormat.parse(strDate);
}

From source file:com.ocs.dynamo.utils.DateUtils.java

/**
 * Creates a java.util.Date based on a string representation
 * /*from ww w.j av  a  2s .  c o m*/
 * @param dateStr
 *            the string (in the format ddMMyyyy)
 * @return
 */
public static Date createDate(String dateStr) {
    if (dateStr == null) {
        return null;
    }

    SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
    try {
        return format.parse(dateStr);
    } catch (ParseException e) {
        throw new OCSRuntimeException(e.getMessage(), e);
    }
}

From source file:controllers.DatasetController.java

public static Result getSearchResult() {
    Form<DataSet> dc = dataSetForm.bindFromRequest();
    ObjectNode jsonData = Json.newObject();
    String dataSetName = "";
    String agency = "";
    String instrument = "";
    String physicalVariable = "";
    String gridDimension = "";
    String startTime = "";
    String endTime = "";
    Date dataSetStartTime = new Date(0), dataSetEndTime = new Date();

    try {//from  w w w  . j  av a  2  s . c om
        dataSetName = dc.field("Dataset Name").value();
        agency = dc.field("Agency").value();
        instrument = dc.field("Instrument").value();
        physicalVariable = dc.field("Physical Variable").value();
        gridDimension = dc.field("Grid Dimension").value();
        startTime = dc.field("Dataset Start Time").value();
        endTime = dc.field("Dataset End Time").value();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");

        if (!startTime.isEmpty()) {
            try {
                dataSetStartTime = simpleDateFormat.parse(startTime);
                Date min = new Date(0);
                Date max = new Date();
                if (dataSetStartTime.before(min)) {
                    dataSetStartTime = min;
                } else if (dataSetStartTime.after(max)) {
                    dataSetStartTime = max;
                }
            } catch (ParseException e) {
                System.out.println("Wrong Date Format :" + startTime);
                return badRequest("Wrong Date Format :" + startTime);
            }
        }

        if (!endTime.isEmpty()) {
            try {
                dataSetEndTime = simpleDateFormat.parse(endTime);
                Date min = new Date(0);
                Date max = new Date();
                if (dataSetEndTime.before(min)) {
                    dataSetEndTime = min;
                } else if (dataSetEndTime.after(max)) {
                    dataSetEndTime = max;
                }
            } catch (ParseException e) {
                System.out.println("Wrong Date Format :" + endTime);
                return badRequest("Wrong Date Format :" + endTime);
            }
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
        Application.flashMsg(APICall.createResponse(ResponseType.CONVERSIONERROR));
    } catch (Exception e) {
        e.printStackTrace();
        Application.flashMsg(APICall.createResponse(ResponseType.UNKNOWN));
    }
    List<DataSet> response = DataSet.queryDataSet(dataSetName, agency, instrument, physicalVariable,
            gridDimension, dataSetStartTime, dataSetEndTime);
    return ok(dataSetList.render(response, dataSetForm));
}

From source file:fm.audiobox.core.utils.ModelUtil.java

/**
 * Given a string representing an UTC date provided by AudioBox server,
 * this method returns a unix timestamp (always in UTC).
 *
 * @param simpleDateFormat the simple date format
 * @param audioboxDate     the string date provided by audiobox server.
 *
 * @return unix timestamp//from  w  w  w.ja v a2 s .c  o m
 *
 * @throws ParseException the parse exception
 */
public static long toUnixTime(SimpleDateFormat simpleDateFormat, String audioboxDate) throws ParseException {

    simpleDateFormat.applyPattern(AUDIOBOX_DATE_FORMAT);
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = simpleDateFormat.parse(audioboxDate);
    return date.getTime();
}