时间格式的转换:时间戳与特定格式时间的转换

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import cn.hutool.core.date.DateUtil;
 
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
public class DateHelper {
    /**
     * @param dateStr yyyy-MM-dd
     * @return
     */
    public static Integer getStartDay(String dateStr) {
        Date date = DateUtil.parse(dateStr);
        long timeStamp = DateUtil.beginOfDay(date).getTime();
        int time = (int) (timeStamp / 1000);
        return time;
    }
 
    /**
     * @param dateStr yyyy-MM-dd
     * @return
     */
    public static Integer getEndDay(String dateStr) {
        Date date = DateUtil.parse(dateStr);
        long timeStamp = DateUtil.endOfDay(date).getTime();
        int time = (int) (timeStamp / 1000);
        return time;
    }
 
    public static String getDateByIngter(Integer dateTime) {
        if (dateTime == null || dateTime <= 0) {
            return "";
        }
        Date date = new Date(dateTime.longValue() * 1000);
        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static Integer getDateTimeStamp() {
        long timeStamp = DateUtil.date().getTime();
        int time = (int) (timeStamp / 1000);
        return time;
    }
 
    public static Integer getDateTimeStampByDate(Date date) {
        long timeStamp = date.getTime();
        int time = (int) (timeStamp / 1000);
        return time;
    }
 
    public static String getDateFormat(Date date) {
        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static Date getStartDate(String dateStr) {
        Date date = DateUtil.parse(dateStr);
        return DateUtil.beginOfDay(date);
    }
 
    public static Date getEndDate(String dateStr) {
        Date date = DateUtil.parse(dateStr);
        return DateUtil.endOfDay(date);
    }
 
    /**
     * 获取次月第一天
     * @return
     */
    public static Integer getFirstDayOfNextMonth() {
        Date date = DateUtil.nextMonth();
        long timeStamp = DateUtil.beginOfMonth(date).getTime();
        int time = (int) (timeStamp / 1000);
        return time;
    }
 
    //获取当月第一天
    public static String getFirstDayOfMonth() {
        Date date = DateUtil.beginOfMonth(new Date());
        return DateUtil.format(date, "yyyy-MM-dd");
    }
 
    //获取当月最后一天
    public static String getLastDayOfMonth() {
        Date date = DateUtil.endOfMonth(new Date());
        return DateUtil.format(date, "yyyy-MM-dd");
    }
 
    //获取指定月份的第一天
    public static String getFirstDayOfMonth(Date date) {
        Date tmp = DateUtil.beginOfMonth(date);
        return DateUtil.format(tmp, "yyyy-MM-dd");
    }
 
    //获取指定月份的最后一天
    public static String getLastDayOfMonth(Date date) {
        Date tmp = DateUtil.endOfMonth(date);
        return DateUtil.format(tmp, "yyyy-MM-dd");
    }
 
    //获取当前时间的下个月
    public static String getNextMonth(Long nowTime) {
        if (nowTime == null || nowTime <= 0) {
            return "";
        }
        String dataTime = DateUtil.format(new Date(nowTime), "yyyy-MM-dd");
        //时间字符串转 LocalDate 类型
        LocalDate today = LocalDate.parse(dataTime);
        //当前月份+(-1)
        today = today.minusMonths(-1);
        //LocalDate日期格式是"YYYY-MM-DD",只需要用toString()就可以转化成字符串类型
        return today.toString();
    }
 
    //获取指定时间的下一年
    public static Long getNextYear(Long nowTime) {
        Calendar calendar   =   new GregorianCalendar();
        Date date = new  Date(nowTime) ;
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        return calendar.getTime().getTime();
    }
 
}

  

posted @   luorx  阅读(177)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示