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 Date stringToDate_yyyyMMdd(String aStrDate) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {//from w w w .  java2 s  .co  m
        if (aStrDate != null)
            return dateFormat.parse(aStrDate);
    } catch (java.text.ParseException ex) {
        return null;
    }
    return null;
}

From source file:Main.java

public static long string2Millis(String str, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    long millis = 0;
    try {//from  www  . j  ava 2  s  . co m
        millis = format.parse(str).getTime();
    } catch (ParseException e) {
    }
    return millis;
}

From source file:Main.java

public static String getLocalDateTimeFromUTCDateTime(String UTCDateTime) {
    java.util.Date nowDate = null;
    String UTCDate = null;//from w  w w .j  av  a  2 s .c  om
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    try {
        nowDate = format.parse(UTCDateTime);
        UTCDate = format.format(nowDate.getTime() + TimeZone.getDefault().getRawOffset());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return UTCDate;

}

From source file:Main.java

public static boolean timeCompare(String beforeTime, String endTime, String format) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    try {// w w  w.j a  v  a  2 s.  co  m
        Date beforeDate = simpleDateFormat.parse(beforeTime);
        long beforeTimeLong = beforeDate.getTime();

        Date endDate = simpleDateFormat.parse(endTime);
        long endTimeLong = endDate.getTime();

        return beforeTimeLong > endTimeLong;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean isOutCurrentTime(String endTime, String format) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    try {/*from  w  w  w .ja  va2s  . c  o  m*/
        Date endDate = simpleDateFormat.parse(endTime);
        long endTimeLong = endDate.getTime();

        Date currDate = new Date(System.currentTimeMillis());
        String currTime = simpleDateFormat.format(currDate);
        currDate = simpleDateFormat.parse(currTime);
        long currentTime = currDate.getTime();

        return currentTime > endTimeLong;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static Date getDateFromString(String format, String dateString) throws ParseException {
    Locale locale = Locale.ENGLISH;
    SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    return sdf.parse(dateString);
}

From source file:Main.java

/**
 * Tries to parse a Date from Bitbucket's scraped HTML source code.
 *
 * @param datetime contents of datetime attribute of time tag
 *        For example 2014-06-06T22:08:03+03:00 or 2015-01-12T20:07:08.658593
 * @return Date or null if parsing fails
 *//*from   w  w w.  j a  va  2s  .c o  m*/
public static Date parseBitbucketDatetime(String datetime) {
    Date date;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    try {
        date = format.parse(datetime);
    } catch (ParseException e1) {
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
        try {
            date = format.parse(datetime);
        } catch (ParseException e2) {
            format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = format.parse(datetime.substring(0, 10));
            } catch (ParseException | IndexOutOfBoundsException e3) {
                // give up and hope these will be provided via API in the future :)
                date = null;
            }
        }
    }
    return date;
}

From source file:Main.java

public static long getTwoDay(String sj1, String sj2) {
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
    long day = 0;
    try {// w  w w.j av a 2 s.c  om
        java.util.Date date = myFormatter.parse(sj1);
        java.util.Date mydate = myFormatter.parse(sj2);
        day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
    } catch (Exception e) {
        return 0;
    }
    return day;
}

From source file:Main.java

private static Boolean currentGreaterThenOld(String currentDate, String oldDate) {
    if (currentDate.isEmpty() || oldDate.isEmpty())
        return false;

    try {//from  ww  w .ja v  a 2s.c  o m
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        Date current = formatter.parse(currentDate);
        Date old = formatter.parse(oldDate);

        if (old.compareTo(current) < 0) {
            Log.d(TAG, "compareDate current Date : " + current.toString() + " is greater then Old Date : "
                    + old.toString());
            return true;
        }

    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    return false;
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
@SuppressWarnings("unchecked")
public static <T> T convert(String string, Class<T> type) {
    try {//from  w ww .j  a  v  a  2s.  co  m
        if (string == null || string.trim().length() == 0) {
            return null;
        }
        if (type.getName().equals("java.util.Date")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            return (T) dateFormat.parse(string);

        }
        if (type.getName().equals("java.sql.Date")) {
            return (T) new java.sql.Date(java.sql.Date.parse(string));
        }
        if (type.getSimpleName().equals("Calendar")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateFormat.parse(string));
            return (T) calendar;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}