Calendar的clear的陷阱

今天在写一个流量控制的模块,要做一个根据输入的时间,获得下一天0点0分0秒的时间戳。


public long caculateResetTimestamp(long currentTimestamp) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTimestamp);
cal.clear(Calendar.MILLISECOND);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.HOUR);
cal.clear(Calendar.HOUR_OF_DAY);
cal.roll(Calendar.DATE, true);

return cal.getTimeInMillis();
}


程序非常简单,习惯性地做了个单元测试。尽然没有通过

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
DailyResetPolicy resetPolicy = new DailyResetPolicy();

@Test
public void testCaculateResetTimestamp() throws ParseException {

long input = toMillis("2010-01-12 12:12:23.234");
long expected = toMillis("2010-01-13 00:00:00.000");
long result = resetPolicy.caculateResetTimestamp(input);

assertEquals(expected, result);
}

long toMillis(String source) throws ParseException {
return format.parse(source).getTime();
}


这怎么可能!!明明应该是对的啊。后来打出时间来,看了下。输出的结果是

2010-01-13 12:00:00.000

是小时域有问题。仔细阅读了JavaDoc后发现,不能使用clear(Calendar.HOUR_OF_DAY)来清除小时域;clear(Calendar.HOUR)也不行。

还是老老实实用set(Calendar.HOUR_OF_DAY,0)来清除小时域。最后干脆全部用set方法了。

正确的版本是这样的:


public long caculateResetTimestamp(long currentTimestamp) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTimestamp);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.roll(Calendar.DATE, true);

return cal.getTimeInMillis();
}
``` <template> <view class="find-container "> <view class="head"> <view class="tab-list2"> <view @tap="changeTab(1,1)" :class="['tab-item2','',activeTab1==1?'active':'']"> <text>待打印</text> </view> <!-- #ifdef MP-WEIXIN --> <view @tap="changeTab(2,1)" :class="['tab-item2','',activeTab1==2?'active':'']"> <text>已打印</text> </view> <!-- #endif --> <view @click="showDrawer('showRight')" :class="['tab-item2']" > <image class="icon" mode="aspectFit" src="@/static/image/search-icon.png"></image> </view> </view> <view class="tab-list " > <view v-for="(item,index) in order_status" @tap="changeTab(1,index)" :class="['tab-item','',activeTab2==index?'active':'']"> <uni-badge class="uni-badge-left-margin" max-num="99999" :is-dot="item.activeTabJiNum==0" :text="item.activeTabJiNum" absolute="rightTop" :offset="[-6, -3]" size="small" v-if="activeTab2==index"><text>{{item.text}}</text></uni-badge> <text v-else>{{item.text}}</text> </view> </view> <view class="tab-list " v-if="activeTab1==2"> <view v-for="(item,index) in team_status" @tap="changeTab(2,index)" :class="['tab-item','',activeTab2==index?'active':'']"> <uni-badge class="uni-badge-left-margin" max-num="99999" :is-dot="item.activeTabJiNum==0" :text="item.activeTabJiNum" absolute="rightTop" :offset="[-6, -3]" size="small" v-if="activeTab2==index"><text>{{item.text}}</text></uni-badge> <text v-else>{{item.text}}</text> </view> </view> </view> <view style="height: 100vh;background-color: rgb(243, 246, 249);flex: 1;z-index: 9999;"> <view style="flex: 1;z-index: 9999;display: flex;">5555</view> </view> <view class="fixed-bottom"> <view style="display: flex;margin-left: 50rpx;float: left;margin-top: 20rpx;font-weight: bold;">打印机:</view> <view style="display: flex;float: right;margin-top: 20rpx;font-weight: bold;padding-right: 10rpx;">无打印机,去连接 ></view> <view style="margin-top: 100rpx;display: flex;margin-left: 70rpx;color: gainsboro;"> <checkbox-group> <label class="checkbox"> <checkbox :value="checked" :checked="checked" /> 全选 </label> </checkbox-group> </view> <view style="display: flex;margin-left: 340rpx;margin-top: -60rpx;"> <view class="btn">立即打印</view> </view> </view> <uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="true" :date="info.date" :insert="false" :lunar="true" :range="true" @confirm="confirm" @close="calendarClose" /> <uni-drawer ref="showRight" mode="right" :mask-click="true" width="300" @change="drchange($event,'showRight')"> <view class="scroll-view"> <scroll-view class="scroll-view-box" scroll-y="true"> <view class="info"> <input class="ipt"placeholder="订单号/快递单号/手机号/寄件人/收件人" placeholderStyle="font-size: 24rpx;" type="text" v-model="mailNo"></input> </view> <view class="close"> <button type="primary" @click="closeDrawer('showRight')"><text class="word-btn-white">搜索</text></button> </view> </scroll-view> </view> </uni-drawer> </view> </template>```<view style="flex: 1;z-index: 9999;display: flex;">5555</view>这个5555不显示出来
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值