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 convertStringToDateBr(String date) {
    Date result = null;// w ww  . j  ava2s .  co m
    if (date.length() == 10) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try {
            result = format.parse(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (date.length() == 23) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        try {
            //Log("afaLog","DATA DO BANCO"+date);
            result = format.parse(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            //Log("afaLog","DATA DO BANCO"+date);
            result = format.parse(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

@Nullable
public static Date parseDate(@NonNull String format, @Nullable String date) {
    if (date == null) {
        return null;
    }//from www .  j a va2 s  . com
    final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
    formatter.setLenient(false);
    try {
        return formatter.parse(date);
    } catch (ParseException e) {
        return null;
    }
}

From source file:Main.java

public static Date getDateTime(String dateTime) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
    Date date = null;/*from w  w w.j a  va  2s  .c  o m*/
    try {
        date = dateFormat.parse(dateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static Calendar stringToCalendar(String date, String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    Calendar cal = GregorianCalendar.getInstance();
    try {/*from w w  w . java2 s  . c  o  m*/
        cal.setTime(dateFormat.parse(date));
    } catch (ParseException e) {
        cal = null;
    }

    return cal;
}

From source file:Main.java

public static String calculateAge(Context context, String date) {
    final String FACEBOOK = "MM/dd/yyyy";
    try {/*from   w w w  . j  a v  a2s  . c  om*/
        SimpleDateFormat sf = new SimpleDateFormat(FACEBOOK, Locale.ENGLISH);
        sf.setLenient(true);
        Date birthDate = sf.parse(date);
        Date now = new Date();
        int age = now.getYear() - birthDate.getYear();
        birthDate.setHours(0);
        birthDate.setMinutes(0);
        birthDate.setSeconds(0);
        birthDate.setYear(now.getYear());
        if (birthDate.after(now)) {
            age -= 1;
        }
        return Integer.toString(age);

    } catch (Exception e) {
        Log.d("DEBUG", "exception in getTwitterDate:");
        e.printStackTrace();
        return "Unknown";
    }
}

From source file:Main.java

/**
 * User friendly date format//from  ww w  . j  a  va  2 s  .c o m
 *
 * @param date
 * */
public static String getDateUserFormat(String date) {
    String formatedDate = "";
    if (date != null) {
        if (date.length() != 0) {
            SimpleDateFormat userFormat = new SimpleDateFormat("d MMM yyyy");
            SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");

            try {

                formatedDate = userFormat.format(sqlFormat.parse(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return formatedDate;
}

From source file:Main.java

public static String calcFrom(String fromDate, String granularity) throws ParseException {
    String pattern = "";
    if (granularity.equals("YYYY-MM-DDThh:mm:ssZ")) {
        pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    } else {//from w  w  w  .jav a  2  s.c o m
        pattern = "yyyy-MM-dd";
    }

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    return sdf.format(sdf.parse(fromDate));

}

From source file:Main.java

public static Date ConvertFromWebService(String strDate) {
    String[] formats = new String[] { "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
            "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd" };
    for (String frm : formats) {
        try {/*from  w  w w .  ja  va  2  s. com*/
            SimpleDateFormat format = new SimpleDateFormat(frm, Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            return format.parse(strDate);
        } catch (Exception ex) {
        }
    }
    return null;
}

From source file:Main.java

public static String formatDate(String date, int minutes) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = null;//from ww w . ja v  a2  s. c  o  m
    try {
        d = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);
    return sdf.format(calendar.getTime());
}

From source file:Main.java

public static long string2Millis(String str, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
    long millis = 0;
    try {/*from w  w  w. j  a  v  a2  s  .co  m*/
        millis = format.parse(str).getTime();
    } catch (ParseException e) {
        Log.e("TAG", e.getMessage());
    }
    return millis;
}