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 date2millis(String pattern, String date) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    long millis = 0L;
    try {//from   ww w  .j ava2  s.co m
        millis = sdf.parse(date).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return millis;
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static String timeFormat(String data) {
    String time = null;//  ww  w  . j ava  2  s .  c  o m
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        time = sdf.format(sdf.parse(data));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return time;
}

From source file:Main.java

public static boolean isLoadAd() {
    long currentTimeMillis = System.currentTimeMillis();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {/*w w  w. ja  v a  2  s . c  o m*/
        long limitTime = simpleDateFormat.parse("2018-04-21").getTime();
        return currentTimeMillis > limitTime;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static long getTime(String dateString) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {//from   w  ww . j a  va 2 s.co  m
        Date date = formatter.parse(dateString);
        return date.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return SystemClock.elapsedRealtime();
}

From source file:Main.java

public static Date convertStringtoDate(String data) {

    if (data == null || data.equals(""))
        return null;

    Date date = null;// w  ww .ja  v a  2s. co m
    try {
        SimpleDateFormat formato = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        date = new java.sql.Date(formato.parse(data).getTime());
    } catch (java.text.ParseException e) {
        date = null;
    }
    return date;
}

From source file:Main.java

/**
 * Parses the given string into a Date using the supported date formats.
 * Returns null if the string cannot be parsed.
 *
 * @param dateString the date string./*from   ww  w. ja v  a  2s. c  o  m*/
 * @return a date.
 */
public static Date parseDate(final String dateString) {
    if (dateString == null) {
        return null;
    }

    for (SimpleDateFormat format : SUPPORTED_DATE_FORMATS) {
        try {
            return format.parse(dateString);
        } catch (ParseException ignored) {
        }
    }

    return null;
}

From source file:Main.java

public static Calendar stringToCalendar(String string) {
    try {/*from  w  ww .  j a  va 2s  .co m*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyy-MM-dd HH:mm", Locale.CHINESE);
        Date date = formatter.parse(string);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date stringToDate(String str, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;/* w w w . j  ava 2 s .c  o m*/
    try {
        date = sdf.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

/**
 * Get month day of week by year, month//ww w. j  a  v  a  2  s  .com
 *
 * @param year
 * @param month
 * @return
 */
public static int getMonthDayOfWeek(int year, int month) {
    Calendar cal = Calendar.getInstance();
    String dateString = year + "-" + (month > 9 ? month : ("0" + month)) + "-01";
    String pattern = "yyyy-MM-dd";
    Date date = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        date = sdf.parse(dateString);
    } catch (ParseException e) {
    }
    if (date != null) {
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return week_index;
    }
    return -1;
}

From source file:Main.java

public static Date stringToDate(String str, String format) {
    Date date = null;/*from  ww  w .  j  a  va 2  s  . c  o m*/
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    try {
        date = formatter.parse(str);
        return date;
    } catch (Exception e) {
        return null;
    }
}