繁体   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