小编Ole*_*.V.的帖子

使用 Spring Rest 服务时在 Date 中获取错误的时间

这是我的 pojo 课

public class TeTripCarDtl implements Serializable {

    private static final long serialVersionUID = -7601044160087552575L;

    @Id
    @Column(name = "CAR_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;

    @Column(name = "TRIP_ID")
    private long tripId;

    @Column(name = "VEHICLE_TYPE")
    private String vehicleType;

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "PICKUP_DATE_TIME")
    private Date pickUpDateTime;// Here I am getting wrong time value

    @Temporal(value = TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @Column(name = "RETURN_DATE_TIME")
    private Date returnDateTime;// Here I am getting wrong time value …
Run Code Online (Sandbox Code Playgroud)

java spring datetime jackson java.util.date

5
推荐指数
1
解决办法
2万
查看次数

SimpleDateFormat toPattern在java 9中的行为有所不同

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("SV", "SE"));
((SimpleDateFormat) dateFormat).toPattern();
Run Code Online (Sandbox Code Playgroud)

在Java 8中,这一行在"yyyy-MM-dd"Java 9中生成"y-MM-dd".

这对我们的遗留代码有一些严重的问题,是否有某种方法可以恢复行为?

java date date-format simpledateformat java-9

5
推荐指数
1
解决办法
945
查看次数

调整scrollIntoView的持续时间

我使用 JS 使用锚标记和 ID 来平滑滚动,并且我想调整平滑滚动动画的持续时间。

这是我的代码:

    let anchorlinks = document.querySelectorAll('a[href^="#"]')
 
    for (let item of anchorlinks) {
        item.addEventListener('click', (e)=> {
            let hashval = item.getAttribute('href')
            let target = document.querySelector(hashval)
            target.scrollIntoView({
                behavior: 'smooth',
                block: 'start',
                duration: 200
            })
            history.pushState(null, null, hashval)
            e.preventDefault()
        })
    }
Run Code Online (Sandbox Code Playgroud)

但是,它仍然使用默认的持续时间。我想不出任何方法来调整动画的持续时间。

谢谢!

javascript animation duration smooth-scrolling

5
推荐指数
0
解决办法
598
查看次数

Java 8日期/时间:即时,无法在索引19处解析

我有以下代码:

String dateInString = "2016-09-18T12:17:21:000Z";
Instant instant = Instant.parse(dateInString);

ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Kiev"));
System.out.println(zonedDateTime);
Run Code Online (Sandbox Code Playgroud)

它给了我以下异常:

线程“主”中的异常java.time.format.DateTimeParseException:无法在java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:的索引19处解析文本'2016-09-18T12:17:21:000Z' 1949)在java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)在java.time.Instant.parse(Instant.java:395)在core.domain.converters.TestDateTime.main(TestDateTime.java:10 )

当我将最后一个冒号更改为句号时:

String dateInString = "2016-09-18T12:17:21.000Z";
Run Code Online (Sandbox Code Playgroud)

…然后执行正常:

2016-09-18T15:17:21 + 03:00 [欧洲/基辅]

因此,问题是-如何使用Instant和解析日期DateTimeFormatter

java time parsing date java.time.instant

4
推荐指数
1
解决办法
6606
查看次数

在Stream中记录

我试图将这段代码转换为Java 8流:

if(list!=null && list.size()>0){
    Actor actor = null;
    for(Actor actor:list){
        log.info("Actor being read: "  actor.getCode());
        List areaList = areaDAO.getArea(actor.getCode());
        if (areaList.size() > 0)
        {
            actor.setArea((String) areaList.get(0));
            log.info("Area{" + areaList.get(0)
                     + "} is fetched for actor{" + actor.getCode() + "}.");
        }
        this.getContext().setReadCount(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何处理这种情况下的日志记录?这是一个好习惯吗?感谢您的帮助.谢谢

java java-8 java-stream

4
推荐指数
2
解决办法
7095
查看次数

从字符串中解析日期时间,偏移量为一位数小时

我需要用 Java 解析日期。我有字符串值2018-05-15 09:32:51.550082 +3:00,我需要将其解析为日期时间。我尝试解析为ZonedDateTime,但在索引 10 处出现错误。尝试使用DateTimeFormatter解析器 'yyyy-MM-dd HH:mm:ss.SSSSSS Z'+3:00进行解析并在索引 26 处出现错误。显然我的 UTC 偏移量无法解析. 我怎样才能做到这一点?

java parsing date datetime-parsing timezone-offset

4
推荐指数
1
解决办法
1149
查看次数

如何转换UTC日期字符串并删除Java中的T和Z?

我正在使用Java 1.7。

尝试转换:

2018-05-23T23:18:31.000Z 
Run Code Online (Sandbox Code Playgroud)

进入

2018-05-23 23:18:31
Run Code Online (Sandbox Code Playgroud)

DateUtils类:

public class DateUtils {

    public static String convertToNewFormat(String dateStr) throws ParseException {
        TimeZone utc = TimeZone.getTimeZone("UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        sdf.setTimeZone(utc);
        Date convertedDate = sdf.parse(dateStr);
        return convertedDate.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用它时:

String convertedDate = DateUtils.convertToNewFormat("2018-05-23T23:18:31.000Z");
System.out.println(convertedDate);
Run Code Online (Sandbox Code Playgroud)

得到以下异常:

Exception in thread "main" java.text.ParseException: Unparseable date: "2018-05-23T23:22:16.000Z"
   at java.text.DateFormat.parse(DateFormat.java:366)
   at com.myapp.utils.DateUtils.convertToNewFormat(DateUtils.java:7)
Run Code Online (Sandbox Code Playgroud)

我可能做错了什么?

有没有更简单的方法(例如Apache Commons lib)?

java utc parseexception java-7 datetime-parsing

4
推荐指数
3
解决办法
1万
查看次数

将字符串转换为 java.time.ZonedDateTime

我想ZonedDateTime在解析数组列表的元素后将它们转换为对象。下面显示了一个字符串。

"2017-02-12 06:59:00 +1300"
Run Code Online (Sandbox Code Playgroud)

目前我使用DateTimeFormatter

DateTimeFormatter dateTimeFormatter =
  DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
Run Code Online (Sandbox Code Playgroud)

并尝试使用parse, 来获取时间:

this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
Run Code Online (Sandbox Code Playgroud)

看下面的方法:

public DateCalculatorTest(String actionTime, int expectedDayOfWeek) {
    DateTimeFormatter dateTimeFormatter = 
      DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
    DateTimeFormatter localTimeFormatter = 
      DateTimeFormatter.ofPattern("YYYY-MM-dd");
    this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
    this.expectedDayOfWeek = expectedDayOfWeek;
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法解析字符串。我收到以下错误:

Text '2017-02-12 06:59:00 +1300' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=12, MonthOfYear=2, OffsetSeconds=46800},ISO resolved to 06:59 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点java.time

java time datetime-parsing java-time zoneddatetime

4
推荐指数
1
解决办法
6827
查看次数

TimeZone.getTimeZone(“ PST”)与TimeZone.getTimeZone(“ America / Los_Angeles”)

我正在使用Java 8,

在我们的代码的早期,sdf.setTimeZone(TimeZone.getTimeZone("PDT"));由于PDT不是有效的ZoneId ,我们曾 用来转换到失败的美国太平洋地区(未引发任何错误,而是转换为默认时区)。

因此,我寻找值中setTimeZone(TimeZone.getTimeZone("PST"));也不可用的那个TimeZone.getAvailableIDs()

最后我最终使用 sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

现在,我们的一位朋友曾经使用setTimeZone(TimeZone.getTimeZone("PST"));过转换为美国太平洋时区,并且转换正常进行。

问题是

TimeZone.getTimeZone("PST");和之间有什么区别TimeZone.getTimeZone("America/Los_Angeles");

哪个更好用?

java timezone jodatime java-8 java-time

4
推荐指数
1
解决办法
633
查看次数

在UTC中使用到期日期时,JWT的到期不起作用

我正在使用jjwtjwt令牌创建。使用本地系统时间设置到期日期时,一切工作正常,即

日期expDate = new Date(new Date()。getTime()+ 180000); //java.util.Date

但是我尝试使用UTC格式的日期时间,并用相同的3分钟到期日期对jwt令牌进行了签名。现在,ExpiredJwtException即使我在创建令牌后也正在进行验证,但是它正在抛出。我正在使用SimpleDateFormat将时区设置为utc。这是我在Java中使用jjwt创建令牌的代码:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date expDate, issDate;
    try {
        expDate = (Date) simpleDateFormat.parse(sdf.format(new Date().getTime() + 180000));
        issDate = (Date) simpleDateFormat.parse(sdf.format(new Date().getTime()));
        JwtBuilder builder = Jwts.builder()
                .setExpiration(expDate)
                .setIssuedAt(issDate)
                .setId(id)
                .signWith(signingKey, signatureAlgorithm);
        jwtToken = builder.compact();
    } catch (ParseException ex) {
    }
Run Code Online (Sandbox Code Playgroud)

令牌已成功创建。我也可以在线验证内容。expDate比issDate早3分钟。我还通过传递已创建的令牌,在调用令牌后立即调用用于验证令牌的方法。我的验证方法有:

    try {
        Jwts.parser().setSigningKey(signingKey).parseClaimsJws(token);
        log.info("jwt verification success");
    } catch (ExpiredJwtException exJwt) {
        log.info("expired jwt : \n{}", exJwt.getMessage());
    } catch …
Run Code Online (Sandbox Code Playgroud)

java date utc jwt jjwt

4
推荐指数
1
解决办法
999
查看次数