Java 从日期字符串中提取时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3504986/
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
Extract time from date String
提问by JJunior
How can I format the "2010-07-14 09:00:02"
date string to depict just "9:00"
?
如何格式化"2010-07-14 09:00:02"
日期字符串以仅描述"9:00"
?
采纳答案by BalusC
Use SimpleDateFormat
to convert between a date string and a real Date
object. with a Date
as starting point, you can easily apply formatting based on various patterns as definied in the javadoc of the SimpleDateFormat
(click the blue code link for the Javadoc).
用于SimpleDateFormat
在日期字符串和真实Date
对象之间进行转换。以Date
为起点,您可以轻松地应用基于 Javadoc 中定义的各种模式的格式SimpleDateFormat
(单击 Javadoc 的蓝色代码链接)。
Here's a kickoff example:
这是一个启动示例:
String originalString = "2010-07-14 09:00:02";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString);
String newString = new SimpleDateFormat("H:mm").format(date); // 9:00
回答by MikeTheReader
I'm assuming your first string is an actual Date object, please correct me if I'm wrong. If so, use the SimpleDateFormat object: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html. The format string "h:mm" should take care of it.
我假设你的第一个字符串是一个实际的 Date 对象,如果我错了,请纠正我。如果是这样,请使用 SimpleDateFormat 对象:http: //download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html。格式字符串 "h:mm" 应该处理它。
回答by Adam
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-07-14 09:00:02");
String time = new SimpleDateFormat("H:mm").format(date);
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
回答by Petr Pudlák
A very simple way is to use Formatter
(see date time conversions) or more directly String.format
as in
一个非常简单的方法是使用Formatter
(参见日期时间转换)或更直接地,String.format
如
String.format("%tR", new Date())
回答by Bohdan Kolesnyk
If you have date in integers, you could use like here:
如果你有整数日期,你可以在这里使用:
Date date = new Date();
date.setYear(2010);
date.setMonth(07);
date.setDate(14)
date.setHours(9);
date.setMinutes(0);
date.setSeconds(0);
String time = new SimpleDateFormat("HH:mm:ss").format(date);
回答by BHAVIK PANCHAL
let datestring = "2017-02-14 02:16:28"
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.full
formatter.timeStyle = DateFormatter.Style.full
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let date = formatter.date(from: datestring)
let date2 = formatter.String(from: date)
回答by Ole V.V.
The other answers were good answers when the question was asked. Time moves on, Date
and SimpleDateFormat
get replaced by newer and better classes and go out of use. In 2017, use the classes in the java.time
package:
当问题被问到时,其他答案都是很好的答案。在每次移动,Date
并SimpleDateFormat
获得通过更新更好的替代课程,去使用了。2017年使用java.time
包中的类:
String timeString = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))
.format(DateTimeFormatter.ofPattern("H:mm"));
The result is the desired, 9:00
.
结果是想要的,9:00
。