如果月份是3个字母的字符串,如何计算2个日期之间的年数 [英] How to calculate the number of years between 2 dates if the month is a 3 letter string

查看:128
本文介绍了如果月份是3个字母的字符串,如何计算2个日期之间的年数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的2个日期,其中月份是3个字母的字符串

I have 2 dates in the format below where the month is a 3 letter string


1988-Jul-21

1988-Jul-21

2016-Dec-18

2016-Dec-18

如何计算日期之间的年数在上面的格式?

How do I calculate the number of years between the dates in the above format?

我能够计算月份是使用Joda时间整数的月份之间的差异,如下所示

I was able to calculate the difference between the dates in years when the month is an integer using Joda time like below

val y = Years.yearsBetween(new LocalDate("1988-12-21"), new LocalDate("2016-1-18"))
println( y.getYears)
Output: 27


推荐答案

您使用 DateTimeFormat 调用 LocalDate.parse(String,DateTim eFormatter) 方法。类似的东西,

You use a DateTimeFormat to invoke the LocalDate.parse(String, DateTimeFormatter) method. Something like,

val y = Years.yearsBetween(
        LocalDate.parse("1988-Jul-21", DateTimeFormat.forPattern("yyyy-MMM-dd")),
        LocalDate.parse("2016-Dec-18", DateTimeFormat.forPattern("yyyy-MMM-dd")))

这篇关于如果月份是3个字母的字符串,如何计算2个日期之间的年数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆