如何在java android studio中将“2017-04-26T20:55:00.000Z”字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43639502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert "2017-04-26T20:55:00.000Z" string to a Date in java android studio
提问by G. Doe
I am new to android studio and java development.
我是 android studio 和 java 开发的新手。
I would like to parse this date:
我想解析这个日期:
"2017-04-26T20:55:00.000Z"
, which I am getting from a hash map string.
,这是我从哈希映射字符串中获取的。
I would like to display only the date on my view.
我只想在我的视图中显示日期。
采纳答案by Jeff.H
You could use substring like so
你可以像这样使用子字符串
String date = yourString.substring(0, 10);
This would pull all the characters from 0 to 10 in your String and save it as a new String.
这会将字符串中的所有字符从 0 拉到 10 并将其保存为新字符串。
In this case that would return "2017-04-26"
在这种情况下,将返回“2017-04-26”
回答by Epiliptik
In my opinion the cleanest way to do it :
在我看来,最干净的方法是:
- extract the date as described here : Java string to date conversion
- create a string containing the formatted date you want thanks to another DateFormat
- display it!
- 按照此处所述提取日期:Java string to date conversion
- 由于另一个 DateFormat,创建一个包含您想要的格式化日期的字符串
- 显示它!
回答by Samarth
You can always use Java's DateFormat
API for achieving this. Here is the code snippet that will help you to achieve whatever task you are looking for.
您始终可以使用 Java 的DateFormat
API 来实现这一点。以下代码片段将帮助您完成您正在寻找的任何任务。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = dateFormat.parse("2017-04-26T20:55:00.000Z");//You will get date object relative to server/client timezone wherever it is parsed
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
String dateStr = formatter.format(date);
You will get date object from which you can use it whichever way you would like to display using date formatter to format again.
您将获得日期对象,您可以从中以任何方式使用它,使用日期格式化程序再次格式化。