ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java8中的时间处理

2019-03-02 12:03:15  阅读:214  来源: 互联网

标签:return 处理 public int 时间 Date static LocalDate java8


java8中关于时间的处理整理

package com.xb.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Author Mr. Guo
 * Create 2019/2/22 - 13:59
 */
public class dateFormatUtils {
    //获取当天的日期(2019-02-26)
    public static LocalDate getCurrentDate = LocalDate.now();
    //获取当前的时间(18:41:32)
    public static LocalTime getCurrentTime = LocalTime.now().withNano(0);

    //指定时间
    public static LocalTime time(int hour, int minute, int second) {
        return LocalTime.of(hour, minute, second);
    }

    public static LocalTime time2(String time) {
        return LocalTime.parse(time);
    }

    //当前时间增加x小时
    public static LocalTime nowTimePlusxHour(int incr) {
        return dateFormatUtils.getCurrentTime.plusHours(incr);
    }

    public static LocalTime nowTimePlusxHosr(int incr) {
        return dateFormatUtils.getCurrentTime.plus(incr, ChronoUnit.HOURS);
    }

    //生日检查或账单日检查
    public static boolean judgeBirthday(int year, int month, int day, int cyear, int cmonth, int cday) {
        LocalDate birthday = LocalDate.of(year, month, day);
        MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay today = MonthDay.from(LocalDate.of(cyear, cmonth, cday));
        return today.equals(birthdayMd);
    }

    //比较当前日期是否在指定日期之后
    public static boolean isAfterOrBefore(int year, int month, int day) {
        LocalDate today = LocalDate.now();
        LocalDate specifyDate = LocalDate.of(year, month, day);
        return today.isAfter(specifyDate);
    }

    //获取当前的时区(Asia/Shanghai)
    public static ZoneId currentZone = ZoneId.systemDefault();

    //查看指定时区的当前时间
    public static LocalDateTime pointedDateTime(String zone) {
        return LocalDateTime.now(ZoneId.of(zone));
    }

    //获取带有时区的时间(2019-02-26T19:21:51.669+08:00[Asia/Shanghai])
    public static ZonedDateTime zonedDateTime(String zone) {
        return ZonedDateTime.now(ZoneId.of(zone));
    }

    //比较两个日期的时间差
    public static int periodDate(int syear, int smonth, int sday, String condition) {
        LocalDate today = LocalDate.now();
        LocalDate specifyDate = LocalDate.of(syear, smonth, sday);
        Period period = Period.between(specifyDate, today);
        if ("day".equals(condition))
            return (int) specifyDate.until(today, ChronoUnit.DAYS);
        if ("month".equals(condition))
            return period.getMonths();
        if ("year".equals(condition))
            return period.getYears();
        return -1;
    }

    //日期时间格式解析、格式化
    public static LocalDate dateParse(String specifyDate) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE;
        return LocalDate.parse(specifyDate, dateTimeFormatter);
    }

    //转为自定义格式的时间
    public static String dateTimeFormatter(String regxDate, int cyear, int month, int cday, boolean ConverCurrentDate) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(regxDate);
        if (ConverCurrentDate)
            return dateTimeFormatter.format(LocalDate.now());
        return dateTimeFormatter.format(LocalDate.of(cyear, month, cday));
    }

    //时间类与Date类的相互转换
    public static Instant DateConverInstant() {
        Instant instant = Instant.now();
        Date date = Date.from(instant);
        Instant instant1 = date.toInstant();
        return instant1;
    }

    //date转换为LocalDateTime
    public static LocalDateTime DateToLocalDateTime() {
        Date date = new Date();
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date LocalDateTimeToDate() {
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    //LocalDate转Date
    //LocalDate不包含时间,所以转换date的时候,会默认转为当天时间的,00:00:00
    public static Date LocalDateToDate() {
        LocalDate localDate = LocalDate.now();
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

}

标签:return,处理,public,int,时间,Date,static,LocalDate,java8
来源: https://www.cnblogs.com/Gxiaobai/p/10460394.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有