当前位置:   article > 正文

日期时间问题汇总 带Z带T时间_java带t z的时间转换

java带t z的时间转换

一。java获取日期

参考

LocalDateTime startDateTime = LocalDateTime.of(2020, 07, 13, 9, 0, 0);
LocalDateTime endDateTime = LocalDateTime.of(2020, 07, 13, 12, 0, 0);

  • 1
  • 2
  • 3

二。mysql获取日期

now()
sysdate
CURDATE()
  • 1
  • 2
  • 3

三。转换时间格式

//字符串与日期的相互转换
字符串转日期: sdf.parse(str)
日期转字符串: sdf.format(date)


Long日期转为日期格式:
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	long time= System.currentTimeMillis();
	Date date = new Date(time);
	String formatDate = sdf.format(date);


//带有T的日期 转为不带T的日期字符串
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String date = dateFormatter.format(时间);

//日期转为Long
new Date().getTime()


ISO 8601时间(带TZ:2022-02-15T12:30:45.123Z  和时间戳的相互转换
  //时间戳转为ISO 8601日期时间
    public static String timestampToISODateTime(long timestamp) {
        DateTimeFormatter ISO_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
        return dateTime.format(ISO_DATE_TIME_FORMATTER);
    }

    //ISO 8601日期时间转为时间戳
    public static long isoDateTimeToTimestamp(String isoDateTime) {
        DateTimeFormatter ISO_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        LocalDateTime dateTime = LocalDateTime.parse(isoDateTime, ISO_DATE_TIME_FORMATTER);
        Instant instant = dateTime.atZone(ZoneId.of("UTC")).toInstant();
        return instant.toEpochMilli();
    }

    public static void main(String[] args) {
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentTimeMillis);

        String iso8601 = timestampToISODateTime(currentTimeMillis);
        System.out.println(iso8601);

        Long aLong = isoDateTimeToTimestamp(iso8601);
        System.out.println(aLong);

    }




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

四。日期比较

//1.java

方法1  before
if (new Date().before(validTime)) {
  //如果当前日期在生效时间前面
    priceExecOrgDO.setStatus(2);
} 

方法2  date.getTime() - date.getTime()
 DateFormat dateFormat = new SimpleDateFormat("HH:mm");
 String nowTimeStr = "3:28";
 Date nowTime = dateFormat.parse(nowTimeStr);
 Date ruleEndTime = dateFormat.parse(ruleEndTimeStr);
 if (nowTime.getTime() > ruleStartTime.getTime() && nowTime.getTime() < ruleEndTime.getTime()) {)

//2.mysql
//datediff函数,返回值是相差的天数,不能定位到小时、分钟和秒
DATEDIFF(START_TIME,END_TIME)

TIMESTAMPDIFF函数,有参数设置,可以精确到天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND),
使用起来比datediff函数更加灵活。对于比较的两个时间,时间小的放在前面,时间大的放在后面。
select TIMESTAMPDIFF(DAY, '2018-03-20 23:59:00', '2015-03-22 00:00:00');

if test里面只需要!=null 不要 validStartTime != ''不然会报错
 <if test="validStartTime != null   ">and DATEDIFF(a.ppeo_valid_time,#{validStartTime})>=0</if>
 <if test="validEndTime != null  ">and DATEDIFF(#{validEndTime},a.ppeo_valid_time) >=0</if>

判断日期是否当前日期
private boolean compareDate(Date comPareDate) {
     boolean result = false;
     if (comPareDate != null) {
         SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd");
         String nowDay = sf.format(new Date());                //当前日期
         String comPare = sf.format(comPareDate);                //当前日期
         if (nowDay.equals(comPare)) {
             result = true;
         }
     }
     return result;
 }
 
当前日期字符串
private String currentDateString() {
     Date date = new Date();
     String year = String.format("%tY", date);
     String month = String.format("%tm", date);
     String day = String.format("%td", date);
     return year + "." + month + "." + day;
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

五。java计算两时间间隔几天,几小时,几分钟

   private String timeCalculate(String time) {  //2022-05-20 11:31:03
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowTimeStr = sdf.format(System.currentTimeMillis());
            //每天毫秒数
            long nd = 1000 * 24 * 60 * 60;
            //每小时毫秒数
            long nh = 1000 * 60 * 60;
            //每分钟毫秒数
            long nm = 1000 * 60;

            Date nowDate = sdf.parse(nowTimeStr);
            Date date = sdf.parse(time);

            long diff = nowDate.getTime() - date.getTime();

            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;

            if (day == 0 && hour == 0 && min < 1) {
                return "刚刚";
            } else if (day == 0 && hour < 1) {
                return min + "分钟前";
            } else if (day < 1 && hour >= 1) {
                return hour + "小时前";
            } else {
                return day + "天前";
            }
        } catch (ParseException e) {
            return time;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

六。问题处理汇总

1 表单有日期格式提交不了 控制器加上如下
 @InitBinder public void initBinder(WebDataBinder binder) {
 	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
  }


2 保存插入的数据没有时分秒
jdbcType指定为TIME的时候,数据库保存为 时分秒 (测试时发现保存了一个1970-1-1的年份)
jdbcType 不指定或者指定为TIMESTAMP 的时候,数据库保存为 年月日时分秒   默认
jdbcType 指定为DATE的时候,数据库只会保存 年月日


3.前台不能正常显示  实体类的属性加上这个
@JsonFormat(pattern="yyyy-MM-dd")   @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
 
 
4.前台显示的时分秒都为0
在resultmapper加 jdbcType=TIMESTAMP


5.Mon Sep 09 14:53:51 CST 2019
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  区分大小写  "yyyy-MM-dd"区分大小写
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

七。带Z带T时间

1、获取前一天的最早时刻和最晚时刻(带ZT格式)
    public void movePriceExecOrgList() {
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取前一天日期
        LocalDate yesterday = today.minusDays(1);
        // 获取前一天的最早时刻(00:00:00)
        LocalDateTime startOfDay = yesterday.atStartOfDay();
        // 获取前一天的最晚时刻(23:59:59)
        LocalDateTime endOfDay = yesterday.atTime(LocalTime.MAX);
        // 格式化为带Z的时间字符串(ISO 8601格式)
        String startZTime = startOfDay.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        // 格式化为带T的时间字符串
        String startTTime = startOfDay.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        log.info("startZTime:{}", startZTime);
        log.info("startTTime:{}", startTTime);
        // 格式化为带Z的时间字符串(ISO 8601格式)
        String endZTime = endOfDay.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        // 格式化为带T的时间字符串
        String endTTime = endOfDay.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        log.info("endZTime:{}", endZTime);
        log.info("endTTime:{}", endTTime);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/572140
推荐阅读
相关标签
  

闽ICP备14008679号