简体   繁体   English

Concat两个字符串然后在Java中转换为日期

[英]Concat Two Strings then Convert to Date in Java

I need to concat two strings together in Java and then format the string and make it a Date object. 我需要在Java中将两个字符串连接在一起,然后格式化字符串并使其成为Date对象。

The two strings that I have at the moment are 31/01/2012 and 20:00 and I want to do something like: 我目前的两个字符串是31/01/201220:00 ,我想做的事情如下:

try {
  DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
  Date result = new Date();
  String tempDate = date + " " + f;
  result = formatter.parse(tempDate);
} catch (ParseException e) {
  ...etc
}

Can someone help me figure how to add the two strings together. 有人可以帮我解决如何将两个字符串添加到一起。 Any help would be appreciated! 任何帮助,将不胜感激!

Your concatenation is fine. 你的连接没问题。 Yet it seems that your format is "dd/MM/yyyy HH:mm" 但似乎你的格式是"dd/MM/yyyy HH:mm"

The code looks good. 代码看起来不错。 Just correct the format it should be 只需更正它应该的格式

"dd/MM/yyyy HH:mm" “dd / MM / yyyy HH:mm”

tl;dr TL;博士

LocalDateTime.of( 
    LocalDate.parse( 
        "31/01/2012" , 
        DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
    ) , 
    LocalTime.parse( "20:00" ) 
).atZone( ZoneId.of( "Africa/Tunis" ) ) 

2012-01-31T20:00+01:00[Africa/Tunis] 2012-01-31T20:00 + 01:00 [非洲/突尼斯]

See this code run live at IdeOne.com . 请参阅IdeOne.com上的代码

Using java.time 使用java.time

You are using troublesome old date-time classes, now supplanted by the java.time classes. 您正在使用麻烦的旧日期时间类,现在由java.time类取代。

You need not concatenate the strings. 您不需要连接字符串。 Parse each, one as a LocalDate and one as a LocalTime . 解析每个,一个作为LocalDate ,一个作为LocalTime

DateTimeFormatter fDate = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.format( "31/01/2012" , fDate ) ;

LocalTime lt = LocalTime.parse( "20:00" ) ; // Built-in formatter handles standard ISO 8601 formats.

You can combine those two parts into LocalDateTime . 您可以将这两个部分组合到LocalDateTime A LocalDateTime has no concept of time zone nor offset-from-UTC . LocalDateTime没有时区概念,也没有UTC的偏移量

LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;

We have no indication of offset-from-UTC nor time zone. 我们没有指示偏离UTC和时区。 So we do not have a specific moment, on an idea about possible moments. 所以我们没有特定的时刻,关于可能的时刻的想法。 For example, 8 PM comes much earlier in New Zealand ( Pacific/Auckland ) than it does in India ( Asia/Kolkata ), and comes hours later in France ( Europe/Paris ). 例如,新西兰( Pacific/Auckland )晚上8点比印度( Asia/Kolkata )早得多,几小时后在法国( Europe/Paris )出现。

To determine a specific moment, we must put that LocalDateTime in the context of a time zone. 要确定特定时刻,我们必须将LocalDateTime放在时区的上下文中。 Perhaps you intended that moment for UTC. 也许你打算在UTC那个时刻。

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

Or perhaps you meant that for Québec time. 或者也许你的意思是魁北克时代。

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . continent/region的格式指定适当的时区名称 ,例如America/MontrealAfrica/CasablancaPacific/Auckland Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). 切勿使用3-4字母缩写,例如ESTIST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM