簡體   English   中英

Android 中的日期解析 yyyy-MM-dd'T'HH:mm:ss

[英]Date parsing yyyy-MM-dd'T'HH:mm:ss in Android

我的字符串日期 --> 2016-10-02T00:00:00.000Z 我只想從這個字符串中獲取日期。 我試圖解析下面的代碼,但它拋出了我的錯誤! 我的格式與字符串中提到的格式完全相同。 任何答案?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        try {
            Date myDate = sdf.parse( dateofJoining.replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
            System.out.println("Date only"+ myDate );
        } catch (ParseException e) {
            e.printStackTrace();
        }

我也厭倦了下面的代碼,

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

我得到的錯誤

java.text.ParseException: Unparseable date: "2016-10-02T00:00:00.000Z" (at offset 19)
05-12 00:18:36.613 4330-4330/com.vroom.riderb2b W/System.err:     at java.text.DateFormat.parse

更改要使用的簡單日期格式: yyyy-MM-dd'T'HH:mm:ss.SSSZ

在您的代碼中:

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");  
try {  
    Date date = format.parse(dtStart.replaceAll("Z$", "+0000"));  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

如果您想從中獲取date/mm/yy

用:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.i("DATE", sdf.format(date));   //previous date object parsed

如果你想要輸出格式: hour:minute AM/PM

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(date));

編輯

更簡單的選擇是將字符串split兩部分,例如:

String dateString = "2016-10-02T00:00:00.000Z";
String[] separated = dateString.split("T");
separated[0]; // this will contain "2016-10-02"
separated[1]; // this will contain "00:00:00.000Z"

試試這個:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

嘗試

Calendar calendar = Calendar.getInstance();
TimeZone localTZ = calendar.getTimeZone();    
String format1 = "yyyy-MM-dd"; //will return 2017-01-31
String format2 = "dd"; //will return DAY only like 31
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTZ );
String result = sdf.format(your_date);

對我來說,它是這樣工作的:

textView_last_comm.setText(parseDateFormat(passDetailsModel.getLast_comm(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "dd/MM/yy HH:mm"));

  public static String parseDateFormat(String dateToFormat, String inputFormat, String outputFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormat);

Date date = null;
String str = null;

try {
    date = inputFormat.parse(dateToFormat);
    str = outputFormat.format(date);
} catch (ParseException e) {
    e.printStackTrace();
}
return str;
}

您可以使用String.substring()方法輕松獲取date

String string = "2016-10-02T00:00:00.000Z";
String date = string.substring(0, 10);

Log.d("SUCCESS", "DATE: " + date);

輸出:

D/SUCCESS: DATE: 2016-10-02

暫無
暫無

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

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