
DateTime
5星
- 浏览量: 0
- 大小:None
- 文件类型:JAVA
简介:
该资源采用包命名法$com.aapoint.util$。
导入所需的时间类:$DateTimeType := java.time localizedDateTime;
导入时间格式解析器:$DateFormatType := java.time.format.DateTimeFormatter;
设置时间调整策略:$AdjustmentStrategy := java.time.temporal.TemporalAdjusters
该类实现了处理本地时间的功能。
检查给定的两个时间点的时间顺序。
@param localDateTime1
@param localDateTime2
@return
public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){
return localDateTime1.isBefore(localDateTime2);
}
* 计算指定时间前后月的第一天
* @param i 指定距离当前月份的时间偏移量
* @param state 状态字段:0表示当前月,1表示上个月,2表示下个月
* @return
*
* public static String firstDay(Integer state, Integer i) {
LocalDateTime date = null;
type 类型字段:0.月,1.天,2.小时,3.分钟,4.秒
date = getLocalDateTime(state, 0, i);
获取该月份的第一天
String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern(yyyy-MM-dd));
输出结果显示为:第一天为: + firstDay;
return firstDay;
}
* 获取当前月前后相邻月中的最后一天
* @param i 指定相对于当前月份的偏移时间:0表示同一个月,-1表示前一个月,+1表示后一个月
* @param state 状态:状态定义为:
- 0 表示当月需要获取该月的最后一天
- 1 表示需要获取前一月的最后一天
- 2 表示需要获取后一月的最后一天
* @return 获取指定月份中的最后一天并返回
*
public static String lastDay(Integer state, Integer i) {
LocalDateTime date = null;
type 类型:
- 0 表示月
- 1 表示天
- 2 表示小时
- 3 表示分钟
- 4 表示秒
date = getLocalDateTime(state, 0, i);
获取该月份的最后一天,并以yyyy-MM-dd格式输出
String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern(yyyy-MM-dd));
输出结果并返回
System.out.println(最后一天为: + lastDay);
return lastDay;
}
* 根据指定状态和距离当前月的时长获取日期
* @param i 表示与当前月相差的月份数量
* @param state 状态标识符:0表示当月,1表示前一个月,2表示后一个月
* @return 返回类型为String的日期字符串
*
public static String obtainDay(Integer state,Integer i){
LocalDateTime date = null;
type 类型标识符:0.月;1.天;2.小时;3.分钟;4.秒
date = getLocalDateTime(state,1,i);
获取指定时间的日期部分
String day = date.format(DateTimeFormatter.ofPattern(yyyy-MM-dd));
输出结果为:获取的时间为(天): +day;
return day;
}
* 获取当前月份前后时间段的小时数
* @param i 指定相对于当前月的时间差(天)
* @param state 状态:0为本月,-1为上个月,+1为下个月
* @return 返回该时间范围内的小时数值字符串
*
public static String obtainHours(Integer state, Integer i) {
LocalDateTime date = null;
int type = typeTypes[state]; // 类型类型:0.月、1.天、2.小时、3.分钟、4.秒
date = getLocalDateTime(type, 2, i); // 根据指定的类型和参数获取日期
*
* 获取该月份的最后一天
* String hours = date.format(DateTimeFormatter.ofPattern(HH:mm:ss)); // 使用格式化字符串获取小时部分
*
系统.out.println(获取的时间为(小时): + hours);
return hours;
}
* 获取当前时间点前后的时间段(时段)
* @param i 设置距离当前月份的时段偏移量
* @param state 状态码:0为当月,1为前一个月,2为后一个月
* @return 获取该时间段结束时的时间点
*
public static String obtainMinutes(Integer state, Integer i) {
LocalDateTime date = null;
type 类型 0.月 1.天 2.小时 3.分钟 4.秒
date = getLocalDateTime(state, type, i);
计算出当前时间段结束时的时间点
String minutes = date.format(DateTimeFormatter.ofPattern(HH:mm:ss));
系统输出获取的时间为(分钟):+minutes;
返回该时间段的总分钟数。
}
注:type 类型分为:月、天、小时、分钟和秒。
* 获取指定时间前后的时间间隔(小时)
* @param i 指定距离当前月份的时间间隔:0表示当月,1表示前一个月,2表示后一个月
* @param state 状态字段:0.当月 1.前一个月 2.后一个月
* @return
*
public static String obtainSeconds(Integer state, Integer i) {
LocalDateTime date = null;
type 类型字段:0代表月、1代表天、2代表小时、3代表分钟、4代表秒
date = getLocalDateTime(state, 4, i);
// 计算并获得该月日期中的最后一天
String seconds = date.format(DateTimeFormatter.ofPattern(HH:mm:ss));
System.out.println(获取的时间为: + seconds);
return seconds;
}
public static void main(String[] args) {
System.out.println(当前时间为: + LocalDateTime.now().format(DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss)));
System.out.println(前一个月份的第一天为: + LocalDateTimeUtil.firstDay(1, 1));
System.out.println(前一个月份的最后一天为: + LocalDateTimeUtil.lastDay(1, 1));
System.out.println(当前时间的前一天为: + LocalDateTimeUtil.obtainDay(1, 1));
System.out.println(当前时间的后一天为: + LocalDateTimeUtil.obtainDay(2, 1));
System.out.println(当前时间的前一小时为: + LocalDateTimeUtil.obtainHours(1, 1));
System.out.println(当前时间的后一小时为: + LocalDateTimeUtil.obtainHours(2, 1));
System.out.println(当前时间的前一分钟为: + LocalDateTimeUtil.obtainMinutes(1, 1));
System.out.println(当前时间的后一分钟为: + LocalDateTimeUtil.obtainMinutes(2, 1));
System.out.println(当前时间的前一秒为: + LocalDateTimeUtil.obtainSeconds(1, 1));
System.out.println(当前时间的后一秒为: + LocalDateTimeUtil.obtainSeconds(2, 1));
}
private static LocalDateTime getLocalDateTime(Integer state, Integer type, Integer i) {
LocalDateTime date;
if (state等于0) {
date = LocalDateTime.now();
} else if (state等于1) {
switch(type) {
case 0: // 取月
date = LocalDateTime.now().扣除几个月时间(i);
break;
case 1: // 取天
date = LocalDateTime.now().扣除几天时间(i);
break;
case 2: // 取小时
date = LocalDateTime.now().扣除几个小时(i);
break;
case 3: // 取分钟
date = LocalDateTime.now().扣除几分钟时间(i);
}
}
return date;
}
全部评论 (0)


