2014-02-15 17 views
3

提取的日期和时间,我这样的形式获得的日期和时间为StringTIMESTAMP从MySQL从服务器:如何从一个字符串时间戳在Java

2014-02-15 05:18:08 

我想是提取日期格式为DD-MM-YYYY格式,时间格式为HH:MM:SS AM/PM。此时间戳的时区不同,我希望在印度时区(IST)。

请记住timestampString数据类型。

+0

使用'SimpleDateFormat'实例进行分析。 – qqilihq

+0

顺便说一下,避免使用3个字母的时区代码。它们既不标准也不唯一。您使用“IST”可能意味着“爱尔兰标准时间”以及“印度标准时间”。相反,请使用[亚洲/加尔各答]等[适当时区名称](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)。 –

回答

5

使用java.text.SimpleDateFormatjava.util.TimeZone

哪个时区的日期字符串是?与时区替换下面UTC时区

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 
Date date = sdf.parse("2014-02-15 05:18:08"); 

SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a"); 
sdf2.setTimeZone(TimeZone.getTimeZone("IST")); 
String dateStr = sdf2.format(date); // Output: 15-02-2014 10:48:08 AM 

注:在哪种格式的小时是在(24小时/ 12小时),在你输入的字符串?上面的例子假设它是24小时格式,因为输入字符串中没有AM/PM信息。

如果输入字符串也是12小时格式,那么您的输入字符串应该提及AM/PM信息,例如2014-02-15 05:18:08 PM。在这种情况下,修改sdfnew SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")

======================== 编辑: ======== =============

要回答你的下一个问题的意见“如何分别提取日期和时间” ......

SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy"); 
sdfDate.setTimeZone(java.util.TimeZone.getTimeZone("IST")); 

SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss a"); 
sdfTime.setTimeZone(java.util.TimeZone.getTimeZone("IST")); 

String dateStr = sdfDate.format(date); 
String timeStr = sdfTime.format(date); 
+0

感谢Yatendra的帮助。它完美地工作。实际上,我对java很陌生。你能告诉我怎样才能提取日期和时间作为一个字符串?其实我是Android编程,我想在它自己的textviews中显示日期和时间。 – Rakesh

+0

好吧,只需改变格式字符串即可。谢谢你,它帮了我很多。谢谢 ! – Rakesh

0

您可以使用DATE_FORMAT(日期,格式)。

在你的情况会是这样的:

SELECT DATE_FORMAT(timestamp, '%e-%c-%Y') FROM table WHERE... 

-edit:上面的代码将返回您的时间戳: “DD-MM-YYYY”。

timestamp是您的mySQL字段(换句话说:列)。

其他格式选项,我建议你有一个快速浏览一下: DATE_FORMAT options

0

使用的SimpleDateFormat,如: -

String s = new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(yourTimestamp); 

欲了解更多信息 SimpleDateFormat

0

接受的answer by Yatendra Goel是正确的。

乔达时间

为了好玩,这里是一个使用Joda-Time 2.3库中相同类型的代码。

请注意,Joda-Time现在在maintenance mode。该团队建议迁移到java.time。代码java.time的代码见my other Answer

FYI ...印度是五个和一半小时提前UTC/GMT。因此,下面的输出有三十分钟的差异。

String input = "2014-02-15 05:18:08"; 
input = input.replace(" ", "T"); // Replace space in middle with a "T" to get ISO 8601 format. 

// Parse input as DateTime in UTC/GMT. 
DateTime dateTimeUtc = new DateTime(input, DateTimeZone.UTC); 
// Adjust to India time. 
DateTimeZone timeZone = DateTimeZone.forID("Asia/Kolkata"); 
DateTime dateTime = dateTimeUtc.withZone(timeZone); 

// Using "en" for English here because (a) it is irrelevant in our case, and (b) I don't know any Indian language codes. 
java.util.Locale localeIndiaEnglish = new Locale("en", "IN"); // (language code, country code); 
DateTimeFormatter formatter = DateTimeFormat.forStyle("SS").withLocale(localeIndiaEnglish).withZone(timeZone); 
String output = formatter.print(dateTime); 

DateTimeFormatter formatterDateOnly = DateTimeFormat.forPattern("dd-MM-yyyy").withLocale(localeIndiaEnglish).withZone(timeZone); 
DateTimeFormatter formatterTimeOnly = DateTimeFormat.forPattern("hh:mm:ss a").withLocale(localeIndiaEnglish).withZone(timeZone); 
String dateOnly = formatterDateOnly.print(dateTime); 
String timeOnly = formatterTimeOnly.print(dateTime); 

转储到控制台...

System.out.println("input: " + input); 
System.out.println("dateTimeUtc: " + dateTimeUtc); 
System.out.println("dateTime: " + dateTime); 
System.out.println("output: " + output); 
System.out.println("dateOnly: " + dateOnly); 
System.out.println("timeOnly: " + timeOnly); 

运行时...

input: 2014-02-15T05:18:08 
dateTimeUtc: 2014-02-15T05:18:08.000Z 
dateTime: 2014-02-15T10:48:08.000+05:30 
output: 15/2/14 10:48 AM 
dateOnly: 15-02-2014 
timeOnly: 10:48:08 AM 
0

TL;博士

ZonedDateTime zdt = LocalDateTime.parse("2014-02-15 05:18:08".replace(" " , "T")).atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.of("Asia/Kolkata")) ; 
LocalDate ld = zdt.toLocalDate(); 
LocalTime lt = zdt.toLocalTime(); 

使用对象,而不是字符串

您应该从数据库中检索日期时间值作为日期时间对象而不是字符串。

关于ResultSet接口,请参见getTimestamp,getDate和类似的方法。

java.time

java.time Java中的类8,后来取代了麻烦的旧的遗留日期时间类,以及与第三方乔达时库。

解析字符串

如果你坚持这样的字符串,使用java.time类解析它。其他答案正在使用与最早版本的Java捆绑在一起的麻烦的旧日期时间类。那些现在是遗留的,应该避免。

你的输入字符串几乎是标准的ISO 8601格式。仅仅用T替换中间的空格。

String input = "2014-02-15 05:18:08".replace(" " , "T") ; 

LocalDateTime

解析为LocalDateTime为字符串没有任何信息关于offset-from-UTC或时区。

LocalDateTime ldt = LocalDateTime.parse(input) ; 

OffsetDateTime

我会假设你输入的字符串值的目的是要在UTC时区一个瞬间。所以调整为UTC。

OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC) ; 

ZonedDateTime

你问这个被调整到India time区,为协调世界时的五个半小时。

atZoneSameInstant表示得到的ZonedDateTime表示与OffsetDateTime相同的同时刻。两者不同之处仅在于他们通过wall-clock time的两个不同镜头观看同一时刻。

ZoneId z = ZoneId.of("Asia/Kolkata"); 
ZonedDateTime zdt = odt.atZoneSameInstant(z); 

LocalDate & LocalTime

如果你想与日期部分和时间的日部分独立工作,提取各为Local…

LocalDate ld = zdt.toLocalDate(); 
LocalTime lt = zdt.toLocalTime(); 

生成字符串表示

toString的所述类别的所有方法产生使用标准ISO 8601格式的字符串表示。要使用其他格式,请使用DateTimeFormatter类。搜索堆栈溢出了很多示例和讨论。

最简单的方法是让课程自动为你本地化。指定一个Locale作为期望的人类语言和所需的文化标准来决定诸如大写,缩写等问题。

Locale locale = new Locale("en" , "IN"); // English language, India cultural norms. 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale); 
String output = zdt.format(f); 

关于java.time

java.time框架是建立在Java 8和更高版本。这些课程取代了老的麻烦日期时间课程,如java.util.Date,.Calendar,& java.text.SimpleDateFormat

Joda-Time项目现在在maintenance mode,建议迁移到java.time。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。

大部分的java.time功能后移植到Java 6 和ThreeTenABP还适于Android

ThreeTen-Extra项目将java.time扩展到其他类。这个项目是未来可能增加java.time的一个试验场。

相关问题