java8

遇到的问题

parallelStream并行计算有线程安全问题

解析2019-03-03 10:00:01.0时间字符串

java8之前是SimpleDateFormat中的parse方法进行解析。
但是在java8中使用DateTimeFormatter的parse方法进行解析时不会成功,因为java8中增加了判断pos.getIndex() < text.length()

if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length()))

解决

 public static void main(String[] args) {
        ParsePosition pos = new ParsePosition(0);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String a= dateTimeFormatter.format(dateTimeFormatter.parseUnresolved("2019-03-03 10:00:01.0", pos));
        System.out.println(a);
}
获取当前系统时间,精确到秒,年-月-日 时-分-秒
public static void main(String[] args) {
       	LocalDateTime localDate = LocalDateTime.now(ZoneId.systemDefault());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateTimeFormatter.format(localDate));
 }

Console:
2019-04-17 14:28:07

判断两个时间的大小,精确到秒,年-月-日 时-分-秒
public static void main(String[] args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTimeBig = LocalDateTime.parse("2019-02-24 22:01:00", dateTimeFormatter);
        LocalDateTime localDateTimeSmall = LocalDateTime.parse("2019-02-24 22:00:00", dateTimeFormatter);
        if(localDateTimeBig.isAfter(localDateTimeSmall)){
            System.out.println(true);
        }
 }

Console:
true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值