我从服务器以这种格式从MySQL获取String
TIMESTAMP
的日期和时间:
2014-02-15 05:18:08
我想要的是以DD-MM-YYYY
格式提取日期和以HH:MM:SS AM/PM
格式提取时间。此时间戳的时区也不同,我希望它在印度时区(IST)。
请记住timestamp
属于String
数据类型。
答案 0 :(得分:5)
使用java.text.SimpleDateFormat
和java.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
。在这种情况下,请将sdf
修改为new 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);
答案 1 :(得分:1)
接受的answer by Yatendra Goel是正确的。
为了好玩,这里使用Joda-Time 2.3库的代码类型相同。
请注意,Joda-Time现在位于maintenance mode。该团队建议迁移到java.time。有关my other Answer代码,请参阅java.time。
仅供参考......印度比UTC /格林尼治标准时间早五个小时小时。因此,下面的输出有三十分钟的差异。
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
答案 2 :(得分:0)
您可以使用DATE_FORMAT(日期,格式)。
在你的情况下它会是这样的:
SELECT DATE_FORMAT(timestamp, '%e-%c-%Y') FROM table WHERE...
-edit:上面的代码会将您的时间戳返回为:“DD-MM-YYYY”。
时间戳是你的mySQL字段(换句话说:列)。
对于其他格式选项我建议您快速查看: DATE_FORMAT options
答案 3 :(得分:0)
使用SimpleDateFormat,如: -
String s = new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(yourTimestamp);
了解更多信息 SimpleDateFormat
答案 4 :(得分:0)
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();
您应该从数据库中检索日期时间值作为日期时间对象而不是字符串。
从JDBC 4.2及更高版本开始,我们可以将 java.time 对象与数据库进行交换。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
myPreparedStatement.setObject( … , instant ) ;
检索。
Instant instant = myResultSet.getObject( … , Instant.class ) ;
Java 8及更高版本中的java.time类取代了麻烦的旧遗留日期时间类以及第三方Joda-Time库。
java.sql.Timestamp
类被Instant
替换。java.sql.Date
类被LocalDate
替换。java.sql.Time
类被LocalTime
替换。如果您遇到这样的String,请使用java.time类解析它。其他答案正在使用与最早版本的Java捆绑在一起的麻烦的旧日期时间类。这些都是遗产,应该避免。
您的输入字符串几乎采用标准ISO 8601格式。仅使用T
替换中间的SPACE。
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区域,该区域比UTC早5个半小时。
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
类。搜索Stack Overflow以获取许多示例和讨论。
最简单的方法是让课程自动为您定位。为所需的人类语言和所需的文化规范指定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 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和& SimpleDateFormat
现在位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*
类。
从哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如Interval
,YearWeek
,YearQuarter
和more。