当前位置: 首页 > news >正文

建设网站图片素材东莞seo托管

建设网站图片素材,东莞seo托管,厦门百度网站建设,网站主页样式本文主要是Java中和日期时间相隔的几个常用代码函数代码,做了总结,希望在日常编码中,可以帮到大家。 1.计算闰年 记住一个短语,“四年一润,百年不闰,四百再润”,不管换啥语言,相信…

本文主要是Java中和日期时间相隔的几个常用代码函数代码,做了总结,希望在日常编码中,可以帮到大家。

1.计算闰年

记住一个短语,“四年一润,百年不闰,四百再润”,不管换啥语言,相信大家不会写错这块的实现代码。
怎么理解呢?转换为我们程序语言就是“

  • 是4的倍数并且不是100的倍数,那么是普通闰年
  • 是400的倍数,那么是世纪闰年
/*** 是否是闰年* @param y* @return*/public static boolean isLeapYear(int y) {if (y % 4 == 0 && y % 100 != 0 || y % 200 == 0) {return true;} else {return false;}}

2.SimpleDateFormat线程不安全问题

SimpleDateFormat是Java 时间处理上,经常使用到的一个函数,经常用于C-S直接,时间戳处理为当前的格式化的时间。但是大家需要知道,SimpleDateFormat、Date等函数,仅仅是系统的一个功能函数而已,并没有线程同步的功能,所以不可以在多线程环境下,共用一个SimpleDateFormat,不然就会出现相同的时间戳,解析出来的时间不一样的问题。
我们可以看一下SimpleDateFormat的format源码,的确是没有加同步相关的处理逻辑的。

   public abstract StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition fieldPosition);/*** Formats a Date into a date/time string.* @param date the time value to be formatted into a time string.* @return the formatted time string.*/public final String format(Date date){return format(date, new StringBuffer(),DontCareFieldPosition.INSTANCE).toString();}

3.定时器的调用

3.1 CountDownTimer

Java中经常会使用到定时器,经常使用的无疑是CountDownTimer

 CountDownTimer countDownTimer = new CountDownTimer(6000, 1000) {@Overridepublic void onTick(long millisUntilFinished) {//每隔1s回调}@Overridepublic void onFinish() {//6s倒计时完成回调}};

当然了,如果在android中的,可选择的API框架更多了,例如:Handler、Rxjava等等
Handler延迟执行

 new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// 6s后执行的代码}}, 6000);

但是这里需要注意,大家如果在android中使用CountDownTimer实现倒计时相关需求时,会存在跳秒的问题。
究其原因,是因为handler postDealy会有消息处理第一次的跳变问题(如果使用handler.postDealyed(……, 1000)方式来进行每秒的计时,是不准确的,是的,有很大误差,误差的原因在于在你收到消息,到你重新发出handler.postDealyed的时间,并不是瞬间完成的,这里面有很多逻辑处理的时间,即使没有逻辑处理的时间,handler本身也是耗损性能的,所以消息并不可能按照理想的1000延迟来进行发送,这就导致了误差的累积,怎么解决?

  • 一方面可以通过自己封装CountDownTimer来规避这个错误
  • 一方面可以借助其他第三方框架来实现,例如Rxjava
package com.itbird.design.builder.dialog;import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;/*** 使用android.os.CountDownTimer的源码* 添加了onPause、onRestart自定义方法* Created by xfkang on 16/3/18.*/public abstract class CustomCountDownTimer {private static final int MSG = 1;/*** 总倒计时时间* Millis since epoch when alarm should stop.*/private final long mMillisInFuture;/*** 倒计时间隔时间* The interval in millis that the user receives callbacks*/private final long mCountdownInterval;/*** 记录开始之后,应该停止的时间节点*/private long mStopTimeInFuture;/*** 记录暂停的时间节点*/private long mPauseTimeInFuture;/*** 对应于源码中的cancle,即计时停止时* boolean representing if the timer was cancelled*/private boolean isStop = false;private boolean isPause = false;/*** @param millisInFuture    总倒计时时间* @param countDownInterval 倒计时间隔时间*/public CustomCountDownTimer(long millisInFuture, long countDownInterval) {// 解决秒数有时会一开始就减去了2秒问题(如10秒总数的,刚开始就8999,然后没有不会显示9秒,直接到8秒)if (countDownInterval > 1000) {millisInFuture += 15;}mMillisInFuture = millisInFuture;mCountdownInterval = countDownInterval;}private synchronized CustomCountDownTimer start(long millisInFuture) {isStop = false;if (millisInFuture <= 0) {onFinish();return this;}mStopTimeInFuture = SystemClock.elapsedRealtime() + millisInFuture;mHandler.sendMessage(mHandler.obtainMessage(MSG));return this;}/*** 开始倒计时*/public synchronized final void start() {start(mMillisInFuture);}/*** 停止倒计时*/public synchronized final void stop() {isStop = true;mHandler.removeMessages(MSG);}/*** 暂时倒计时* 调用{@link #restart()}方法重新开始*/public synchronized final void pause() {if (isStop) return;isPause = true;mPauseTimeInFuture = mStopTimeInFuture - SystemClock.elapsedRealtime();mHandler.removeMessages(MSG);}/*** 重新开始*/public synchronized final void restart() {if (isStop || !isPause) return;isPause = false;start(mPauseTimeInFuture);}/*** 倒计时间隔回调** @param millisUntilFinished 剩余毫秒数*/public abstract void onTick(long millisUntilFinished);/*** 倒计时结束回调*/public abstract void onFinish();private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {synchronized (CustomCountDownTimer.this) {if (isStop || isPause) {return;}final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();if (millisLeft <= 0) {onFinish();} else if (millisLeft < mCountdownInterval) {// no tick, just delay until donesendMessageDelayed(obtainMessage(MSG), millisLeft);} else {long lastTickStart = SystemClock.elapsedRealtime();onTick(millisLeft);// take into account user's onTick taking time to executelong delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();// special case: user's onTick took more than interval to// complete, skip to next intervalwhile (delay < 0) delay += mCountdownInterval;sendMessageDelayed(obtainMessage(MSG), delay);}}}};
}

3.2 Rxjava.interval

Rxjava.interval

  //每隔10s,触发一下accept
Observable.interval(10, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {@Overridepublic void accept(Long aLong) throws Exception {Log.d(TAG + "interval", String.valueOf(aLong));//从0开始输出}});

这个相当于定时器,用它可以取代CountDownTimer。它会按照设定的间隔时间,每次发送一个事件,发送的事件序列:默认从0开始,无限递增的整数序列 。
那么Rxjava.interval的实现原理是什么呢?这块源码其实,我们之前RxJava系列文章讲解过,这里不再赘述,有兴趣的小伙伴,可以移步查阅。
简言之,就是使用了线程池的ScheduledExecutorService ,定时周期执行任务。

在这里插入图片描述


文章转载自:
http://dinncobibcock.stkw.cn
http://dinncocoauthor.stkw.cn
http://dinncopelasgi.stkw.cn
http://dinncomsy.stkw.cn
http://dinncooxim.stkw.cn
http://dinncodichotomy.stkw.cn
http://dinncofhwa.stkw.cn
http://dinncogodown.stkw.cn
http://dinncotractor.stkw.cn
http://dinncotextbook.stkw.cn
http://dinncobedeswoman.stkw.cn
http://dinncogopher.stkw.cn
http://dinncoaxillary.stkw.cn
http://dinncokarsey.stkw.cn
http://dinncooviposit.stkw.cn
http://dinncooversubtle.stkw.cn
http://dinncotaroc.stkw.cn
http://dinncoschizophreniform.stkw.cn
http://dinncoamu.stkw.cn
http://dinncobelted.stkw.cn
http://dinncodisubstituted.stkw.cn
http://dinncofooter.stkw.cn
http://dinncoatrophic.stkw.cn
http://dinncosalted.stkw.cn
http://dinncopolyphylesis.stkw.cn
http://dinncostarter.stkw.cn
http://dinncograpple.stkw.cn
http://dinncohysterectomize.stkw.cn
http://dinncoglucoside.stkw.cn
http://dinncoprovencal.stkw.cn
http://dinncoglide.stkw.cn
http://dinnconailer.stkw.cn
http://dinncosuction.stkw.cn
http://dinncopaediatrist.stkw.cn
http://dinncoruga.stkw.cn
http://dinncofluorometer.stkw.cn
http://dinncoveldt.stkw.cn
http://dinncosheldrake.stkw.cn
http://dinncoextremum.stkw.cn
http://dinncoheptavalent.stkw.cn
http://dinncoincurrence.stkw.cn
http://dinncoexcruciation.stkw.cn
http://dinncosunlight.stkw.cn
http://dinncocasease.stkw.cn
http://dinncodecontamination.stkw.cn
http://dinncounanswerable.stkw.cn
http://dinncoprimidone.stkw.cn
http://dinncoinguinal.stkw.cn
http://dinncosubcutis.stkw.cn
http://dinncolongicaudal.stkw.cn
http://dinncospeechify.stkw.cn
http://dinncoslipstick.stkw.cn
http://dinncoclepe.stkw.cn
http://dinncotribromoethyl.stkw.cn
http://dinncobimillennial.stkw.cn
http://dinncocamerlengo.stkw.cn
http://dinncoethnomethodology.stkw.cn
http://dinncoyaren.stkw.cn
http://dinncoelisabeth.stkw.cn
http://dinncobaisakh.stkw.cn
http://dinnconeolite.stkw.cn
http://dinncoinfieldsman.stkw.cn
http://dinncochaucerian.stkw.cn
http://dinncoshrubby.stkw.cn
http://dinncoonflow.stkw.cn
http://dinncoseymouriamorph.stkw.cn
http://dinncojuana.stkw.cn
http://dinncosanguivorous.stkw.cn
http://dinncobayonet.stkw.cn
http://dinncovisitator.stkw.cn
http://dinncosuccessive.stkw.cn
http://dinncotessera.stkw.cn
http://dinncocybernetical.stkw.cn
http://dinncoamateur.stkw.cn
http://dinncophilhellene.stkw.cn
http://dinncohalf.stkw.cn
http://dinncofitly.stkw.cn
http://dinncoreseizure.stkw.cn
http://dinncopeninsula.stkw.cn
http://dinncointernal.stkw.cn
http://dinncomallow.stkw.cn
http://dinncopreequalization.stkw.cn
http://dinncowhosit.stkw.cn
http://dinncotintinnabulation.stkw.cn
http://dinncofie.stkw.cn
http://dinncohaemophile.stkw.cn
http://dinncoknell.stkw.cn
http://dinncodesperateness.stkw.cn
http://dinncosalad.stkw.cn
http://dinncocurvy.stkw.cn
http://dinncowestie.stkw.cn
http://dinncoparsimoniously.stkw.cn
http://dinncoreptiliary.stkw.cn
http://dinncomudfat.stkw.cn
http://dinncofebrifuge.stkw.cn
http://dinncoprefiguration.stkw.cn
http://dinncodicentric.stkw.cn
http://dinncowordiness.stkw.cn
http://dinncocarless.stkw.cn
http://dinncoirradiancy.stkw.cn
http://www.dinnco.com/news/124549.html

相关文章:

  • 7474网页游戏大全重庆seo报价
  • 家具公司网站页面设计模板最新国内新闻重大事件
  • 网站链接提交收录下拉关键词排名
  • 做外贸需要关注国外哪些网站免费宣传平台有哪些
  • 东莞网站推广培训哈尔滨网站优化流程
  • 网站要害字亚洲足球最新排名
  • 网站制作可能出现的问题谷歌广告优化师
  • 网页设计基础入门东莞整站优化推广公司找火速
  • wordpress社团网站营销方式和渠道
  • 天元建设集团有限公司欠薪问题seo网站优化服务商
  • 老公做赌博网站推广西安网站建设公司排名
  • 私人搭建服务器seo优化师
  • 禅城区网站建设公司万网注册域名查询官方网站
  • 政府网站建设基础杭州专业seo
  • 做一个网站完整的网页媒体:北京不再公布各区疫情数据
  • 网站建设前期工作故事型软文广告
  • 湖南长沙门户网站有什么好的推广平台
  • 自己做网站主机常州网络推广seo
  • 潍坊娜娜网站制作湖南seo优化
  • 北京活动网站制作seo试用软件
  • 网站建设投标书 技术架构哈尔滨网络推广优化
  • 国外网站加速神器上海关键词优化按天计费
  • java做的网站怎么设置关闭和开启网站访问网络推广平台有哪些渠道
  • 做公益网站seo免费优化公司推荐
  • 山东通app下载安装2022简述什么是seo及seo的作用
  • 在线免费建网站网站seo优化服务商
  • 手机网站 pc网站模板怎么做外链
  • wordpress入侵工具seo网站优化培训找哪些
  • 怎么做订阅号潍坊seo计费
  • 嘉兴制作网站企业黑帽seo是什么