簡體   English   中英

SimpleDateFormat.parse(String)提供空值android

[英]SimpleDateFormat.parse(String) gives null value android

我正在使用通過SimpleDateFormat在UTC中解析日期和時間的android應用程序,但問題是在解析該字符串時我得到了null值。 下面給出了我的代碼,以及我從日期選擇器獲得的日期。

// 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();

                }

您的SimpleDateFormat模式錯誤。 由於您的日期格式

6/24/2015 4:07:00 PM

它應該是

m/dd/yyyy h:mm:ss a

並不是

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

使用以下代碼

    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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM