gpt4 book ai didi

java - 修改日期不修改时间

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:42 25 4
gpt4 key购买 nike

JodaTime 中,不使用 'plus' 或 'minus' 函数并使用最少的代码行,如何在不修改时间的情况下设置新日期?

我的第一次尝试是使用 getHoursOfDay()DateTime 的“时间”部分存储在单独的 int 中getMinutesOfHour() 等 - 然后创建一个包含所需日期的新 DateTime 并再次设置小时、分钟和秒。但这种方法非常笨拙,我想知道是否有更简单的方法来执行此操作 - 最好只用一行代码。

例如:

22/05/2013 13:40:02 >>>> 30/08/2014 13:40:02

最佳答案

JodaTime 是必须的吗?做到这一点的基本方法是1. 从时间戳中提取时间。2. 将此添加到刚刚日期


long timestamp = System.currentTimeMillis(); //OK we have some timestamp
long justTime = timestamp % 1000 * 60 * 60 * 24;// just tiem contains just time part


long newTimestamp = getDateFromSomeSource();//now we have date from some source
justNewDate = newTimestamp - (newTimestamp % 1000 * 60 * 60 * 24);//extract just date

result = justNewDate + justTime;

像这样。

关于java - 修改日期不修改时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com