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 String changeStringToDate2(String str) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date date6 = null;/*from  www .j a  va2 s .c  om*/
    try {
        date6 = sdf.parse(str);
        java.sql.Date date7 = new java.sql.Date(date6.getTime());
        return date7.toString();
    } catch (ParseException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static Calendar string2Calendar(String date) {
    date = getDateNoWeek(date);/*from  w ww . ja  v  a2  s .c  o m*/
    final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date d;
    try {
        d = format.parse(date);
    } catch (ParseException e) {
        d = new Date();
    }
    final Calendar mCalendar = (Calendar) Calendar.getInstance().clone();
    mCalendar.setTime(d);
    return mCalendar;
}

From source file:Main.java

public static String getStringByFormat(String strDate, String format) {
    String mDateTime = null;/*from ww w .ja va  2 s .co  m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
        c.setTime(mSimpleDateFormat.parse(strDate));
        SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
        mDateTime = mSimpleDateFormat2.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

public static String normalizeDate(String rDate) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.US);
    Date date = null;//from  ww  w  . ja va  2  s.c  o  m
    try {
        date = simpleDateFormat.parse(rDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return DateFormat.getDateInstance(DateFormat.LONG).format(date);
}

From source file:Main.java

public static Date getDate(String t, String pattern) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
    try {//from  ww  w . j a v a  2  s.  com
        Date date = dateFormat.parse(t);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        return date;
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public synchronized static String formatDisplayActivityDate(String date) {
    String formattedDateDisplay = "";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dateFormat = null;/*from   ww w  . jav a  2  s .  c  o  m*/
    try {
        dateFormat = sdf.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
    formattedDateDisplay = sdf2.format(dateFormat);

    String[] dateTimeArray = formattedDateDisplay.split("T");

    return formattedDateDisplay;
}

From source file:Main.java

public static Date getNowTimeDate() {
    Date date = new Date();
    SimpleDateFormat fordate = new SimpleDateFormat("yyyy-MM-dd");
    String time = fordate.format(date);
    try {//from w ww  .j  a  v  a2  s . co  m
        date = fordate.parse(time);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date getDate(String dateTime) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    Date date = null;/*from   w  ww. j  a  v  a 2 s.c o  m*/
    try {
        date = dateFormat.parse(dateTime);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Date convertDateStringToDate(String dateString) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    // dateFormat.setLenient(true);

    return dateFormat.parse(dateString);
}

From source file:Main.java

/**
 * Get date by year, month//from  w  w  w.  j a va2 s .c o  m
 *
 * @param year
 * @param month
 * @return
 */
public static String getDate(int year, int month) {
    String datetime = "" + year + (month > 9 ? month : ("0" + month));
    String parrern = "yyyyMM";
    SimpleDateFormat df = new SimpleDateFormat(parrern);
    try {
        Date date = df.parse(datetime);
        SimpleDateFormat df2 = new SimpleDateFormat(DATE_FORMAT);
        return df2.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}