如何将包含日期和时间的String转换为LocalTime变量

时间:2017-09-14 12:02:51

标签: java time simpledateformat java-time localtime

我正在转换包含三个不同时间的String s序列  开始,结束和持续时间
 "00:01:00,00:02:00,00:01:00"

LocalTime变量:

for (final String str : downtime) {
      final DependencyDownTime depDownTime = new DependencyDownTime ();
      final String[] strings = str.split (",");

      if (strings.length == 3) {

           LocalTime start = LocalTime.parse (strings[0]);
           depDownTime.setStartTime (start);

           LocalTime end = LocalTime.parse (strings[1]);
           depDownTime.setEndTime (end);

           Duration duration = Duration.between (start, end);
           depDownTime.setDuration (duration);
           downTimes.add (depDownTime);
      }
  }

正在解析的String已更改,现在包含日期。 2017-09-13 00:01:00

如何删除仅保留时间的日期字符串?

我尝试使用SimpleDateFormat

final DateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss");
LocalTime start = LocalTime.parse (dateFormat.format (strings[0]));
depDownTime.setStartTime (start);

但我得到java.lang.IllegalArgumentException

6 个答案:

答案 0 :(得分:4)

您可以使用DateTimeFormatter,然后将其解析为LocalDateTime并从中提取LocalTime

String input = "2017-09-13 00:01:00";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalTime time = LocalDateTime.parse(input, fmt).toLocalTime();

或使用LocalTime方法直接将其解析为from

LocalTime time = LocalTime.from(fmt.parse(input));

或(甚至更简单):

LocalTime time = LocalTime.parse(input, fmt);

答案 1 :(得分:2)

使用DateTimeFormatter(java.time.format.DateTimeFormatter)和LocalDateTime

main.dart.js:10583 EXCEPTION: NullError: method not found: 'appendChild' on null
STACKTRACE: 
TypeError: Cannot read property 'appendChild' of null
    at EN.$0 (file:///build/web/main.dart.js:9422:3)
    at EU.dart.EU.$0 (file:///build/web/main.dart.js:9393:14)
    at N7.dart.x3.func (file:///build/web/main.dart.js:3058:35)
    at bK.dart.bK.E8 (file:///build/web/main.dart.js:10339:7)
    at Object.eval (eval at qf (file:///build/web/main.dart.js:1851:15), <anonymous>:2:41)
    at Qw.aY (file:///build/web/main.dart.js:4346:12)
    at bK.aY (file:///build/web/main.dart.js:10386:30)
    at pY.aY (file:///build/web/main.dart.js:9314:3)
    at pY.z4 (file:///build/web/main.dart.js:9317:28)
    at file:///home/vitaliy/Apps/web_lko/build/web/main.dart.js:9289:12
ORIGINAL EXCEPTION: NullError: method not found: 'appendChild' on null

(anonymous) @ main.dart.js:10583
main.dart.js:10583 EXCEPTION: FormatException
STACKTRACE: 
FormatException
    at Object.d (file:///build/web/main.dart.js:1720:3)
    at Object.mz (file:///build/web/main.dart.js:1582:37)
    at Object.bL (file:///build/web/main.dart.js:1587:21)
    at file:///build/web/main.dart.js:33670:5
    at W6.a (file:///build/web/main.dart.js:2908:72)
    at W6.dart.W6.$2 (file:///build/web/main.dart.js:3146:24)
    at Vm.dart.Vm.$1 (file:///build/web/main.dart.js:3142:31)
    at N7.dart.x5.func (file:///build/web/main.dart.js:3065:35)
    at bK.dart.bK.Ea (file:///build/web/main.dart.js:10346:7)
    at Object.eval (eval at qf (file:///build/web/main.dart.js:1851:15), <anonymous>:2:43)
ORIGINAL EXCEPTION: FormatException

(anonymous) @ main.dart.js:10583
main.dart.js:1251 Uncaught Error: NullError: method not found: 'appendChild' on null
    at EN.$0 (main.dart.js:9422)
    at EU.dart.EU.$0 (main.dart.js:9393)
    at N7.dart.x3.func (main.dart.js:3058)
    at bK.dart.bK.E8 (main.dart.js:10339)
    at Object.eval (eval at qf (main.dart.js:1851), <anonymous>:2:41)
    at Qw.aY (main.dart.js:4346)
    at bK.aY (main.dart.js:10386)
    at pY.aY (main.dart.js:9314)
    at pY.z4 (main.dart.js:9317)
    at main.dart.js:9289

答案 2 :(得分:1)

如果您确定字符串的格式已经相同,则可以使用String#split

String s = "2017-09-13 00:01:00";
String[] frags = s.split(" ");
System.out.println(frags[frags.length - 1]);

输出:

00:01:00

如果需要,请添加一些检查。

答案 3 :(得分:1)

您需要使用新的java datetime API。即配置正确的解析器。这是:

String original = "2017-09-13 00:01:15";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ') // this is what you won't find among the default formatters
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .toFormatter();
LocalTime time = LocalTime.parse(original, formatter); // it's cool now
System.out.println(time); // prints 00:01:15

答案 4 :(得分:0)

这可能会帮助你::

或者您可以使用另一个答案中所写的分割功能。

此处您的字符串已转换为日期和日历对象。现在你可以对这些对象做任何事了。

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class test {
    public static void main(String[] args) throws IOException, ParseException {
        String timestamp = "2017-09-13 00:01:00";
        DateFormat format = new SimpleDateFormat("yyyy-dd-mm HH:mm:ss");
        Date date = format.parse(timestamp);
        System.out.println(date);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        System.out.println(date.getTime());
        System.out.println(date.getHours());
        System.out.println(date.getMinutes());
        System.out.println(date.getSeconds());
    }
}

答案 5 :(得分:0)

如果格式yyyy-mm-dd hh:mm:ss是固定的,那么到目前为止这可能是最快的答案,因为它避免了任何类型的添加字符串解析或拆分:

LocalTime.parse(strings[0].substring(11))