热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java时间时区处理转换工具类

importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayLis

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;/*** 时间工具类*/
public class MorliaDateUtil {/*** 时间格式为:yyyy-MM-dd HH:mm:ss*/private static final String FORMAT_1 = "yyyy-MM-dd HH:mm:ss";/*** 时间格式为:yyyyMMddHHmmss*/private static final String FORMAT_2 = "yyyyMMddHHmmss";/*** 时间格式为:yyyyMMddHHmmssSSS*/private static final String FORMAT_3 = "yyyyMMddHHmmssSSS";/*** 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm"
* 如果获取失败,返回null*/
public static String getUTCTimeStr() {return getTime(0, FORMAT_1);} /*** 得到UTC时间,类型为字符串,格式为"yyyyMMddHHmmss"* 如果获取失败,返回null*/public static String getUTCTimeStr2() {return getTime(0, FORMAT_2);}/*** 得到北京时间,类型为字符串,格式为"yyyyMMddHHmmss"
* 如果获取失败,返回null*/
public static String getCCTTimeStr2() {return getTime(8, FORMAT_2);}/*** 得到北京时间,类型为字符串,格式为"yyyyMMddHHmmssSSS"
* 如果获取失败,返回null*/
public static String getCCTTimeStr3() {return getTime(8, FORMAT_3);}/*** 获取时间* &#64;param timeZoneInt 时区&#xff0c;-12 到 12的整数&#xff0c;不支持夏令时* &#64;param formatString 自定义时间格式*/public static String getTime(int timeZoneInt, String formatString) {//设置时区&#xff0c;仅限于-12到12的整数时区if(timeZoneInt > 12 || timeZoneInt < -12) {return null;}TimeZone timeZone &#61; TimeZone.getTimeZone("GMT" &#43; (timeZoneInt > 0 ? "&#43;" &#43; timeZoneInt : timeZoneInt) &#43; ":00");// 1、取得本地时间&#xff1a;Calendar cal &#61; Calendar.getInstance(timeZone);int year &#61; cal.get(Calendar.YEAR);int month &#61; cal.get(Calendar.MONTH) &#43; 1;int day &#61; cal.get(Calendar.DAY_OF_MONTH);int hour &#61; cal.get(Calendar.HOUR_OF_DAY);int minute &#61; cal.get(Calendar.MINUTE);int second &#61; cal.get(Calendar.SECOND);int millisecond &#61; cal.get(Calendar.MILLISECOND);formatString &#61; formatString.replace("yyyy", String.valueOf(year));formatString &#61; formatString.replace("MM", String.valueOf(month > 9 ? month : "0" &#43; month));formatString &#61; formatString.replace("dd", String.valueOf(day > 9 ? day : "0" &#43; day));formatString &#61; formatString.replace("HH", String.valueOf(hour > 9 ? hour : "0" &#43; hour));formatString &#61; formatString.replace("mm", String.valueOf(minute > 9 ? minute : "0" &#43; minute));formatString &#61; formatString.replace("ss", String.valueOf(second > 9 ? second : "0" &#43; second));formatString &#61; formatString.replace("SSS", String.valueOf(millisecond > 9 ? millisecond > 99 ? "0" &#43; millisecond : "00" &#43; millisecond : millisecond));return formatString;}/*** 根据开始时间和结束时间&#xff0c;将时间分成一天一天的集合* &#64;param: String starttime,String endtime*/public static List<String> getDateList(String starttime, String endtime){SimpleDateFormat sdf &#61; new SimpleDateFormat("yyyy-MM-dd");Date dateFormat1 &#61; new Date(-7 * 24 * 3600 * 1000L);TimeZone timeZone &#61; TimeZone.getTimeZone("GMT&#43;8:00");SimpleDateFormat currentTime &#61; new SimpleDateFormat("yyyy-MM-dd");currentTime.setTimeZone(timeZone);String currentDate &#61; currentTime.format(new Date());Date dateFormat2 &#61; new Date();try {dateFormat1 &#61; sdf.parse(starttime);dateFormat2 &#61; sdf.parse(endtime).getTime() > sdf.parse(currentDate).getTime()?sdf.parse(currentDate) : sdf.parse(endtime);} catch (ParseException e) {return null;}List<String> list &#61; new ArrayList<String>();while(dateFormat1.getTime() <&#61; dateFormat2.getTime()) {String strs &#61; sdf.format(dateFormat1);list.add(strs);dateFormat1.setTime(dateFormat1.getTime() &#43; 24 * 3600 * 1000L);}return list;}
}

package com.aaa.bbb.utils;import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
&#64;Slf4j
public class ItemDateUtils {private static final String yyyyMMddHHmmssSSS &#61; "yyyyMMddHHmmssSSS";private static final String yyyy_MM_dd_HH_mm_ss&#61;"yyyy-MM-dd HH:mm:ss";private static final String defaultformate&#61;"yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ";public static String get8TimeStr() {return getTime(8, yyyy_MM_dd_HH_mm_ss);}public static Date get8Time(){try {SimpleDateFormat simpleDateFormat &#61; new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT&#43;:08:00"));Date dateValue &#61; simpleDateFormat.parse(get8TimeStr());return dateValue;}catch (Exception e){log.error(e.getMessage(),e);}return null;}/*** 获取时间yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ* &#64;param timeZoneInt 时区&#xff0c;-12 到 12的整数&#xff0c;不支持夏令时* &#64;param formatString 自定义时间格式*/public static String getTime(int timeZoneInt, String formatString) {//设置时区&#xff0c;仅限于-12到12的整数时区if(timeZoneInt > 12 || timeZoneInt < -12) {return null;}TimeZone timeZone &#61; TimeZone.getTimeZone("GMT" &#43; (timeZoneInt > 0 ? "&#43;" &#43; timeZoneInt : timeZoneInt) &#43; ":00");// 1、取得本地时间&#xff1a;Calendar cal &#61; Calendar.getInstance(timeZone);int year &#61; cal.get(Calendar.YEAR);int month &#61; cal.get(Calendar.MONTH) &#43; 1;int day &#61; cal.get(Calendar.DAY_OF_MONTH);int hour &#61; cal.get(Calendar.HOUR_OF_DAY);int minute &#61; cal.get(Calendar.MINUTE);int second &#61; cal.get(Calendar.SECOND);int millisecond &#61; cal.get(Calendar.MILLISECOND);formatString &#61; formatString.replace("yyyy", String.valueOf(year));formatString &#61; formatString.replace("MM", String.valueOf(month > 9 ? month : "0" &#43; month));formatString &#61; formatString.replace("dd", String.valueOf(day > 9 ? day : "0" &#43; day));formatString &#61; formatString.replace("HH", String.valueOf(hour > 9 ? hour : "0" &#43; hour));formatString &#61; formatString.replace("mm", String.valueOf(minute > 9 ? minute : "0" &#43; minute));formatString &#61; formatString.replace("ss", String.valueOf(second > 9 ? second : "0" &#43; second));formatString &#61; formatString.replace("SSS", String.valueOf(millisecond > 9 ?millisecond > 99 ? "0" &#43; millisecond : "00" &#43; millisecond : millisecond));return formatString;}}

推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • JVM 学习总结(三)——对象存活判定算法的两种实现
    本文介绍了垃圾收集器在回收堆内存前确定对象存活的两种算法:引用计数算法和可达性分析算法。引用计数算法通过计数器判定对象是否存活,虽然简单高效,但无法解决循环引用的问题;可达性分析算法通过判断对象是否可达来确定存活对象,是主流的Java虚拟机内存管理算法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
author-avatar
茫茫人海啊啊啊_574
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有