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 getPreTime(String sj1, String jj) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String mydate1 = "";
    try {//from  w ww . j  a v  a 2 s .  com
        Date date1 = format.parse(sj1);
        long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
        date1.setTime(Time * 1000);
        mydate1 = format.format(date1);
    } catch (Exception e) {
    }
    return mydate1;
}

From source file:common.ClassUtils.java

public static void copyBean(Object src, Object dest) {
    try {// w  ww.j  a v a  2s  .  c o  m
        // ?   frombean-->birthday=""  user-birhday=null
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) { //198a
                if (value == null) {
                    return null;
                }
                String s = (String) value;
                if (s.trim().equals("")) {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return sdf.parse(s);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }, Date.class);
        //bean
        BeanUtils.copyProperties(dest, src);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getConverDateString(String date, String fromFormat, String toFormat) {
    if (TextUtils.isEmpty(date))
        return "";
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern(fromFormat);//from   w ww .  j  a  v a2 s  . c  om
    Date d = null;
    try {
        d = sdf.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    sdf.applyPattern(toFormat);
    return sdf.format(d);
}

From source file:Main.java

public static long toTimestamp(String date) {
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    java.util.Date temp;//from  w ww. j a  v  a  2s .  co m

    try {
        temp = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return -1;
    }
    return temp.getTime();

}

From source file:Main.java

public static String timeToDeparture(String startTime) {
    int diffMinutes = -1;
    Calendar now = Calendar.getInstance();
    now.setTime(new Date());
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;/*from  w w w.  j a va  2s  .  c om*/
    try {
        date = dateFormat.parse(startTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    long millisDiff = cal.getTimeInMillis() - now.getTimeInMillis();
    if (millisDiff > 0) {
        diffMinutes = ((int) millisDiff / (60 * 1000));
    }
    return String.valueOf(diffMinutes);
}

From source file:org.sonar.api.charts.AbstractChartTest.java

protected static Date stringToDate(String sDate) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy hh'h'mm");
    return sdf.parse(sDate);
}

From source file:Main.java

public static int getWeekNum() {
    int num = 1;//from  w ww.  ja va 2  s .  co m
    try {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        long to = new Date().getTime();
        long from = df.parse("2014-9-8 00:00:00").getTime();
        num = (int) Math.ceil((to - from) * 1.0 / (1000 * 60 * 60 * 24) / 7.0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return num;
}

From source file:Main.java

public static final Date stringToDate(SimpleDateFormat dateFormat, String dateToConvert, boolean catchError) {
    try {//from  w  ww.  j  a va2 s .co m
        Date localDate = dateFormat.parse(dateToConvert);
        return localDate;
    } catch (Throwable localThrowable) {
        if (!catchError) {
            return new Date(1000L + System.currentTimeMillis());
        }
    }
    return null;
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static Date toDate(String sDate, String sFmt) {
    SimpleDateFormat sdfFrom = null;
    java.util.Date dt = null;//from ww w.j  av a 2 s  .c o  m
    try {
        sdfFrom = new SimpleDateFormat(sFmt);
        dt = sdfFrom.parse(sDate);
    } catch (Exception ex) {
        return null;
    } finally {
        sdfFrom = null;
    }

    return dt;
}

From source file:Main.java

public static Date convertStringToDateSimple(String date) {
    //Log("afaLog", "DATA SAIDA" + date);
    Date result = null;//  w  ww .  j a v  a  2 s.  com
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String data = date.replace(" 00:00:00", "");
    try {
        result = format.parse(date);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}