日期解析yyyy-MM-dd不'HH:mm:ss在Android中。

时间:2022-10-29 13:06:00

My string date --> 2016-10-02T00:00:00.000Z. I want to get only date from this string. I tried to parse through below coding but it throws me error! I have exactly the same format as mentioned in the string. Any answers?

我的字符串日期——2016-10-10 02t00: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();
        }

I also tired below code,

我也厌倦了下面的代码,

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

The error which i get

我得到的误差。

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

5 个解决方案

#1


4  

change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ

更改简单的日期格式:yyyy-MM-dd,不对:mm: sssz。

in your code:

在你的代码:

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

If you want to get date/mm/yy from it:

如果你想从上面得到日期/mm/yy:

use:

使用:

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

if you want output format: hour:minute AM/PM

如果你想要输出格式:小时:分钟AM/PM。

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

EDIT

编辑

More easier option is to split the string in two parts like:

更简单的方法是将字符串分成两部分:

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"

#2


0  

Below is all date formats available, read more doc here.

下面是所有日期格式,请阅读更多的文档。

Symbol  Meaning                 Kind         Example
------------------------------------------------------------------------------------
D       day in year             Number        189
E       day of week             Text          E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
F       day of week in month    Number        2 (2nd Wed in July)
G       era designator          Text          AD
H       hour in day (0-23)      Number        0
K       hour in am/pm (0-11)    Number        0
L       stand-alone month       Text          L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
M       month in year           Text          M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
S       fractional seconds      Number        978
W       week in month           Number        2
Z       time zone (RFC 822)     Time Zone     Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
a       am/pm marker            Text          PM
c       stand-alone day of week Text          c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
d       day in month            Number        10
h       hour in am/pm (1-12)    Number        12
k       hour in day (1-24)      Number        24
m       minute in hour          Number        30
s       second in minute        Number        55
w       week in year            Number        27
G       era designator          Text          AD
y       year                    Number        yy:10 y/yyy/yyyy:2010
z       time zone               Time Zone     z/zz/zzz:PST zzzz:Pacific Standard 

Hope it helps.

希望它可以帮助。

#3


0  

try with this :

试试这个:

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

#4


0  

Try

试一试

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

#5


-1  

You can get date easily by using String.substring() method:

您可以使用String.substring()方法轻松获得日期:

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

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

OUTPUT:

输出:

D/SUCCESS: DATE: 2016-10-02

#1


4  

change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ

更改简单的日期格式:yyyy-MM-dd,不对:mm: sssz。

in your code:

在你的代码:

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

If you want to get date/mm/yy from it:

如果你想从上面得到日期/mm/yy:

use:

使用:

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

if you want output format: hour:minute AM/PM

如果你想要输出格式:小时:分钟AM/PM。

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

EDIT

编辑

More easier option is to split the string in two parts like:

更简单的方法是将字符串分成两部分:

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"

#2


0  

Below is all date formats available, read more doc here.

下面是所有日期格式,请阅读更多的文档。

Symbol  Meaning                 Kind         Example
------------------------------------------------------------------------------------
D       day in year             Number        189
E       day of week             Text          E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
F       day of week in month    Number        2 (2nd Wed in July)
G       era designator          Text          AD
H       hour in day (0-23)      Number        0
K       hour in am/pm (0-11)    Number        0
L       stand-alone month       Text          L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
M       month in year           Text          M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
S       fractional seconds      Number        978
W       week in month           Number        2
Z       time zone (RFC 822)     Time Zone     Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
a       am/pm marker            Text          PM
c       stand-alone day of week Text          c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
d       day in month            Number        10
h       hour in am/pm (1-12)    Number        12
k       hour in day (1-24)      Number        24
m       minute in hour          Number        30
s       second in minute        Number        55
w       week in year            Number        27
G       era designator          Text          AD
y       year                    Number        yy:10 y/yyy/yyyy:2010
z       time zone               Time Zone     z/zz/zzz:PST zzzz:Pacific Standard 

Hope it helps.

希望它可以帮助。

#3


0  

try with this :

试试这个:

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

#4


0  

Try

试一试

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

#5


-1  

You can get date easily by using String.substring() method:

您可以使用String.substring()方法轻松获得日期:

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

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

OUTPUT:

输出:

D/SUCCESS: DATE: 2016-10-02