简体   繁体   中英

SimpleDateFormat.parse(String) gives null value android

I am working on android application in which i am parsing date and time in UTC by using SimpleDateFormat but the problem is that on parsing that string i am getting the null value. My code is given below, along with the date which i got from date pickers.

// It is the date which i get it from pickers mDateSelected = 6/24/2015 4:07:00 PM
Date myDate = null;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                try {

                // Here i am getting null Value ar myDate   
                    myDate = simpleDateFormat.parse(mDateSelected);



                } catch (Exception e1) {
                    e1.printStackTrace();

                }

your SimpleDateFormat pattern is wrong. Since you have a date in the format

6/24/2015 4:07:00 PM

it should be

m/dd/yyyy h:mm:ss a

and not

"yyyy-MM-dd HH:mm:ss"

use these code following below

    String Todaydate;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Todaydate = df.format(new java.util.Date());
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    Log.d("Current Date", Todaydate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM