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 getQuot(String time1, String time2) {
    long quot = 0;
    SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
    try {//from  w  w  w.  ja  v a2s.  c o m
        Date date1 = ft.parse(time1);
        Date date2 = ft.parse(time2);
        quot = date1.getTime() - date2.getTime();
        quot = quot / 1000 / 60 / 60 / 24;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return quot;
}

From source file:Main.java

public static String changeDateFormat(String string) {
    //1986-06-06/*from   www . j  a  va 2  s.  c  o  m*/
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();

    try {
        date = format.parse(string);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    format = new SimpleDateFormat("yyyy/MM/dd");
    return format.format(date);
}

From source file:Main.java

/**
 * Loops over all the possible date formats and tries to find the right one.
 *
 * @param dateValue//from w  ww  .  j  a v  a 2s . c  om
 *            to parse
 * @return a valid {@link Date} or null if none of the formats matched.
 */
public static Date parseDate(String dateValue) {
    if (dateValue == null)
        return null;

    Date date = null;
    for (final SimpleDateFormat format : FORMATS) {
        try {
            final SimpleDateFormat clonedFormat = (SimpleDateFormat) format.clone();
            date = clonedFormat.parse(dateValue);
            break;
        } catch (ParseException e) {
            // We loop through this until we found a valid one.
            //noinspection UnnecessaryContinue
            continue;
        }
    }

    return date;
}

From source file:Main.java

public static Date StrToDate(String str) {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
    Date date = null;/*from w  ww .ja  va2  s .  co  m*/
    try {
        date = format.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return date;
}

From source file:Main.java

public static long formatTime(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Date date;//from  ww  w.  j  a v  a 2s .  c  o m
    try {
        date = sdf.parse(time);
        long millis = date.getTime();
        return millis;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static long getLongFromDate(String date) {

    SimpleDateFormat sdf = new SimpleDateFormat("E,dd MMM yyyy", Locale.US);
    long time = 0;
    try {//from w ww .  j  ava  2 s.  co  m
        time = sdf.parse(date).getTime();
    } catch (ParseException e) {

        e.printStackTrace();

    }
    return time;
}

From source file:Main.java

public static String getJobDateFormat(String strDate, String inputFormat) {
    String formatDate = "";
    String dayFormat[] = strDate.split("-");
    int day = Integer.parseInt(dayFormat[0]);
    try {/* ww w  . j  a va 2  s.c o m*/
        String dayNumberSuffix = getDayNumberSuffix(day);
        SimpleDateFormat inputDateFormat = new SimpleDateFormat(inputFormat, Locale.US);
        Date date = inputDateFormat.parse(strDate);
        SimpleDateFormat outputDateFormat = new SimpleDateFormat(" d'" + dayNumberSuffix + "' MMMM yyyy",
                Locale.US);
        formatDate = outputDateFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return formatDate;
}

From source file:Main.java

/**
 * Convert date into milliseconds./*from   w  w  w .  j a  v  a  2 s. c  o m*/
 * 
 * @param crisisDate
 * @return
 */
public static long getMiliseconds(String crisisDate) {
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date date = (Date) formatter.parse(crisisDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static Calendar getDateFromString(String string, String format) {
    Calendar calendar = null;/*w w w  .  j  a v  a2 s  .  c  o  m*/

    SimpleDateFormat form = new SimpleDateFormat(format, Locale.US);
    Date date = null;

    try {
        date = form.parse(string);

        calendar = Calendar.getInstance();
        calendar.setTime(date);
    } catch (java.text.ParseException e) {
    }

    return calendar;
}

From source file:Main.java

public static String getUTCDateTimeFromLocalDateTime(String localTimeStr) {
    java.util.Date nowDate = null;
    String UTCDate = null;/*from   w w  w. j av a2  s.  c  o m*/
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    try {
        nowDate = format.parse(localTimeStr);
        UTCDate = format.format(nowDate.getTime() - TimeZone.getDefault().getRawOffset());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return UTCDate;

}