持续更新(19.2.14)
萌新无聊写的日期类,说不定哪一天会用到
序号 | 主要功能 | 参数 | 返回结果例子 |
---|---|---|---|
1 | 计算出当前年份还剩多少天 单位:天 | - | 231 |
2 | 计算出现在已经过了今年的百分之几 范围(0.0d~1.0d) | - | 0.01268574 |
3 | 显示现在的季节(不规范,按照个人喜好规定) | - | 春 |
显示指定日期的季节(不规范) | 17-10-4 , yy-M-d | 秋 | |
4 | 生成日期时间 | - | 2019/2/14 18:12:29 |
M/d HH:mm:ss | 2/14 18:12:29 | ||
5 | 计算2天之间的距离 | 2016/12/4 , 2017/12/5 | 366 |
16/12/4 , 17/12/5 , yy/M/d , DateUtils.DAY | 366 |
为了便捷这里出一个来自网上的常用日期格式对照表
字母 | 含义 | 示例 |
---|---|---|
y | 年份。一般用 yy 表示两位年份,yyyy 表示 4 位年份 | 使用 yy 表示的年扮,如 11; 使用 yyyy 表示的年份,如 2011 |
M | 月份。一般用 MM 表示月份,如果使用 MMM,则会 根据语言环境显示不同语言的月份 | 使用 MM 表示的月份,如 05; 使用 MMM 表示月份,在 Locale.CHINA 语言环境下,如“十月”;在 Locale.US 语言环境下,如 Oct |
d | 月份中的天数。一般用 dd 表示天数 | 使用 dd 表示的天数,如 10 |
D | 年份中的天数。表示当天是当年的第几天, 用 D 表示 | 使用 D 表示的年份中的天数,如 295 |
E | 星期几。用 E 表示,会根据语言环境的不同, 显示不 同语言的星期几 | 使用 E 表示星期几,在 Locale.CHINA 语 言环境下,如“星期四”;在 Locale.US 语 言环境下,如 Thu |
H | 一天中的小时数(0~23)。一般用 HH 表示小时数 | 使用 HH 表示的小时数,如 18 |
h | 一天中的小时数(1~12)。一般使用hh表 示小时数 | 使用 hh 表示的小时数,如 10 (注意 10 有 可能是 10 点,也可能是 22 点) |
m | 分钟数。一般使用 mm 表示分钟数 | 使用 mm 表示的分钟数,如 29 |
s | 秒数。一般使用 ss 表示秒数 | 使用 ss 表示的秒数,如 38 |
S | 毫秒数。一般使用 SSS 表示毫秒数 | 使用 SSS 表示的毫秒数,如 156 |
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期时间工具类
* 1.生成日期的指定格式
* 2.日起计算
*/
public class DateUtils {
/*选择返回计算日期的单位*/
public static final int DAY = 120001;
public static final int HOUR = 120002;
public static final int MINUTE = 120003;
public static final int SECOND = 120004;
public static final int WEEK = 120005;
/**
* 计算出今年还剩多少天
* @return
*/
public static int lastForYear() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int today = cal.get(Calendar.DAY_OF_YEAR);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 366 - today;
}else {
return 365 - today;
}
}
/**
* 计算出现在已经过了今年的多少%
* @return double
*/
public static double proForYear() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int today = cal.get(Calendar.DAY_OF_YEAR);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return today / 366.0;
}else {
return today / 365.0;
}
}
/**
* 生成现在的季节
* @return
*/
public static String genSeason(){
// Date d = new SimpleDateFormat("yyyy-M-d").parse(date);
int month = Calendar.getInstance().get(Calendar.MONTH);
// int month = d.getMonth() + 1;
switch (month + 1){
case 2:
case 3:
case 4:return "春";
case 5:
case 6:
case 7:return "夏";
case 8:
case 9:
case 10:return "秋";
case 11:
case 12:
case 1:return "冬";
default:return "未知";
}
}
/**
* 生成指定日期的季节
* @param date 指定日期
* @param format 日期格式
* @return 季节字符串
*/
public static String genSeason(String date,String format){
try {
Date d = new SimpleDateFormat(format == null ? "yyyy-M-d" : format).parse(date);
int month = d.getMonth() + 1;
switch (month){
case 2:
case 3:
case 4:return "春";
case 5:
case 6:
case 7:return "夏";
case 8:
case 9:
case 10:return "秋";
case 11:
case 12:
case 1:return "冬";
default:return "未知";
}
} catch (ParseException e) {
e.printStackTrace();
return "未知";
}
}
/**
* 生成日期时间
* @return 字符串 - yyyy/MM/dd
*/
public static String genMomentDate() {
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
}
/**
* 生成日期时间 - 高级
* @param format 返回指定格式
* @return -> format
*/
public static String genMomentDate(String format) {
format = format == null ? "yyyy/MM/dd HH:mm:ss" : format;
return new SimpleDateFormat(format).format(new Date());
}
/**
* 计算2天的距离
*
* @param beginDate 开始日期
* @param endDate 结束日期
* @return 天
*/
public static int resultDays(String beginDate, String endDate) {
int finalDate = 0;
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-M-d");
Date d1 = sd.parse(beginDate);
Date d2 = sd.parse(endDate);
final int resultDate = (int) ((d2.getTime() - d1.getTime()) / (60 * 60 * 1000 * 24));
finalDate = resultDate;
} catch (Exception e) {
e.printStackTrace();
}
return finalDate;
}
/**
* 计算2天的距离 - 高级使用
* @param beginDate 开始日期
* @param endDate 结束日期
* @param format 传入日期格式,如空为默认
* @param returnType 返回单位
* @return -> returnType
*/
public static int resultDays(String beginDate, String endDate, String format, int returnType) {
try {
SimpleDateFormat sd = new SimpleDateFormat(format == null ? "yyyy-M-d" : format);
Date d1 = sd.parse(beginDate);
Date d2 = sd.parse(endDate);
long resultDate = (d2.getTime() - d1.getTime());
switch (returnType) {
case WEEK:
return (int) (resultDate / (1000 * 60 * 60 * 24 * 7));
case DAY:
return (int) (resultDate / (1000 * 60 * 60 * 24));
case HOUR:
return (int) (resultDate / (1000 * 60 * 60));
case MINUTE:
return (int) (resultDate / (1000 * 60));
case SECOND:
return (int) (resultDate / (1000));
//返回类型错误
default:
return 0;
}
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}