首页 > 其他分享 >日期转换-面板1

日期转换-面板1

时间:2022-11-17 21:26:15浏览次数:36  
标签:return param Date 日期 date 转换 Calendar 面板 calendar



import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* Date 工具类
*
*/
public class DatePubTool {

public static final String beginTime = " 00:00:00";

public static final String endTime = " 23:59:59";

public static Long getTime(Long year, Long month, Long day) {
Calendar calendar = Calendar.getInstance();
if (null != year) {
calendar.set(Calendar.YEAR, year.intValue());
}
if (null != month && 0 != month) {
calendar.set(Calendar.MONTH, month.intValue() - 1);
} else {
calendar.set(Calendar.MONTH, 0);
}
if (null != day && 0 != day) {
calendar.set(Calendar.DAY_OF_MONTH, day.intValue());
} else {
calendar.set(Calendar.DAY_OF_MONTH, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();
return date.getTime() / 1000;
}

/**
* 查询传入日期所属周的第一天 (周一为第一天)
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getWeekFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
if (null == date)
return null;
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, 2);
return calendar.getTime();
}

/**
* 查询周日的时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getWeekLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (Calendar.DAY_OF_WEEK == calendar.getFirstDayOfWeek()) {
// 如果刚好是周日,直接返回
return date;
} else {
// 如果不是周日,加一周计算
calendar.add(Calendar.DAY_OF_YEAR, 7);
calendar.set(Calendar.DAY_OF_WEEK, 1);
return calendar.getTime();
}
}

/**
* 格式化日统计表数据库时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String getDayFormatTime(Date date) {
if (null == date)
return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);
}

/**
* 查询当天起始时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String getCurDayBeginTime(Date date) {
if (null == date)
return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date) + beginTime;
}

/**
* 查询当天结束时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String getCurDayEndTime(Date date) {
if (null == date)
return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date) + endTime;
}

/**
* 得到本月第一天的日期
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getMonthFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
if (null == date)
return null;
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}

/**
* 得到本月最后一天的日期
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getMonthLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}

/**
* 得到本季度第一天的日期
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getFirstDayOfQuarter(Date date) {
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH) {
cDay.set(Calendar.MONTH, Calendar.JANUARY);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE) {
cDay.set(Calendar.MONTH, Calendar.APRIL);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
cDay.set(Calendar.MONTH, Calendar.JULY);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.OCTOBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));
return cDay.getTime();
}

/**
* 得到本季度最后一天的日期
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getLastDayOfQuarter(Date date) {
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH) {
cDay.set(Calendar.MONTH, Calendar.MARCH);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE) {
cDay.set(Calendar.MONTH, Calendar.JUNE);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {
cDay.set(Calendar.MONTH, Calendar.AUGUST);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.DECEMBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
return cDay.getTime();
}

/**
* 返回第几个月份,不是几月 季度一年四季, 第一季度:2月-4月, 第二季度:5月-7月, 第三季度:8月-10月, 第四季度:11月-1月
*
* @param month
* @param isQuarterStart
* @return
* @see [类、类#方法、类#成员]
*/
public static int getQuarterInMonth(int month, boolean isQuarterStart) {
int months[] = { 1, 4, 7, 10 };
if (!isQuarterStart) {
months = new int[] { 3, 6, 9, 12 };
}
if (month >= 2 && month <= 4)
return months[0];
else if (month >= 5 && month <= 7)
return months[1];
else if (month >= 8 && month <= 10)
return months[2];
else
return months[3];
}

/**
* 得到一月中第几天
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static int getDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
if (null == date)
return 0;
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}

/**
* 得到一个月总天数
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static int getMonthDays(Date date) {
Calendar calendar = Calendar.getInstance();
if (null == date)
return 0;
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

/**
* 得到两个日期的时间差 (精确到秒)
*
* @param startDate
* 开始时间
* @param endDate
* 结束时间
* @return
* @see [类、类#方法、类#成员]
*/
public static int getIntervalDays(Date startDate, Date endDate) {
if (null == startDate || null == endDate) {
return -1;
}
long intervalMilli = endDate.getTime() - startDate.getTime();
return (int) (intervalMilli / (24 * 60 * 60 * 1000)) + 1;
}

/**
* 得到两个日期的时间差 (精确到天)
*
* @param startDate
* 开始时间
* @param endDate
* 结束时间
* @return
* @see [类、类#方法、类#成员]
*/
public static int daysOfTwo(Date startDate, Date endDate) {
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(startDate);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(endDate);
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
}

/**
* 获取指定日期在当年的所在的周数
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static int getWeekOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
}

/**
* 获取指定日期在的年份
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
return year;
}

/**
* 获取指定日期在的月份
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String getMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("MM");
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(date);
// int month = calendar.get(Calendar.MONTH) + 1;
return sdf.format(date);
}

/**
* 判断是否是当天时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isCurrentDay(Date date) {
Date day = new Date();
Date dayBegin = dayBegin(day);
Date dayEnd = dayEnd(day);
return date.after(dayBegin) && date.before(dayEnd);
}

/**
* 获取日期当天开始时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date dayBegin(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}

/**
* 获取日期当天结束时间
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date dayEnd(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}

/**
* 判断某一时间是否在一个区间内
*
* @param starTime
* 区间开始时间
* @param endTime
* 区间结束时间
* @param curTime
* 当前时间
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isInTime(Date starTime, Date endTime, Date curTime) {
long now = curTime.getTime();
long start = starTime.getTime();
long end = endTime.getTime();
if (end < start) {
if (now >= end && now < start) {
return false;
} else {
return true;
}
} else {
if (now >= start && now < end) {
return true;
} else {
return false;
}
}
}

/**
* 判断选择的日期是否是设置时间所在的周
*
* @param date
* @param time
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean isThisWeek(Date date, long time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
calendar.setTime(new Date(time));
int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
if (paramWeek == currentWeek) {
return true;
}
return false;
}

/**
* 获取某年某周第一天日期
*
* @param year
* @param week
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.WEEK_OF_YEAR, week);
return getFirstDayOfWeek(c.getTime());
}

/**
* 取得指定日期所在周的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
}

/**
* 查询日期是周内第几天
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Long getWhichDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setFirstDayOfWeek(Calendar.MONDAY);
int tmp = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (0 == tmp) {
tmp = 7;
}
return Long.valueOf(tmp);
}

public static Long convertYmd(String ymd) {
try {
if (StringUtils.isEmpty(ymd)) {
return null;
}
String ymdStr = String.valueOf(ymd);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(ymdStr);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

public static Long convertMonthYmd(String ymd) {
try {
if (StringUtils.isEmpty(ymd)) {
return null;
}
String ymdStr = String.valueOf(ymd);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date date = sdf.parse(ymdStr);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

public static Long convertYear(String year) {
try {
if (StringUtils.isEmpty(year)) {
return null;
}
String ymdStr = String.valueOf(year);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = sdf.parse(ymdStr);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

/**
* 查询月出第一天日期
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getMonthFirstDay(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
return cal.getTime();
}

/**
* 得到某年某月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
return cal.getTime();
}

/**
* 查詢一個月的天數
*
* @param year
* @param month
* @return
* @see [类、类#方法、类#成员]
*/
public static int getCountDayOfMonth(int year, int month) {
// 获取当前时间
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

public static LinkedHashMap<String, String> getMap(Date date, int type, Long startTime, Long endTime) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
switch (type) {
case 1:
for (int i = 0; i < 7; i++) {
map.put(String.valueOf(i + 1), "0");
}
break;
case 2:
int days = getMonthDays(date);
if (days > 0) {
for (int i = 1; i <= days; i++) {
map.put(String.valueOf(i), "0");
}
}
break;
case 3:
for (int i = 0; i < 12; i++) {
map.put(String.valueOf(i + 1), "0");
}
break;
case 4:
// 开始时间转换
long startConverTime = startTime * 1000L;
Date startDate = new Date(startConverTime);
// 结束时间转换
long endConverTime = endTime * 1000L;
Date endDate = new Date(endConverTime);
int startYear = getYear(startDate);
int endYear = getYear(endDate);
for (int i = startYear; i <= endYear; i++) {
map.put(String.valueOf(i), "0");
}
break;
default:
break;
}
return map;
}

public static List<Date> getBetweenDates(Calendar startDay, Calendar endDay) {
List<Date> result = new ArrayList<Date>();
// 给出的日期开始日比终了日大则不执行打印
if (startDay.compareTo(endDay) >= 0) {
return result;
}
result.add(startDay.getTime());
Calendar currentPrintDay = startDay;
while (true) {
// 日期加一
currentPrintDay.add(Calendar.DATE, 1);
// 日期加一后判断是否达到终了日,达到则终止打印
if (currentPrintDay.compareTo(endDay) >= 0) {
break;
}
result.add(currentPrintDay.getTime());
}
result.add(endDay.getTime());
return result;
}

/**
*
* Description: <br>
*
* @author 黄浩 <br>
* @taskId <br>
* @param date
* <br>
* @param format
* <br>
* @return <br>
*/
public static Date string2Date(String date, String format) {
if (ObjectUtils.isEmpty(format)) {
throw new IllegalArgumentException("the date format string is null!");
}
DateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date.trim());
} catch (ParseException e) {
throw new IllegalArgumentException("the date string " + date + " is not matching format: " + format, e);
}
}

/**
*
* Description: <br>
*
* @author xulei <br>
* @taskId 两个时间相差的天数,非精确时间<br>
* @param date
* <br>
* @param format
* <br>
* @return <br>
*/
public static int daysDistance(Date fDate, Date oDate) {

/*
* Calendar aCalendar = Calendar.getInstance();
*
* aCalendar.setTime(fDate);
*
* int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
*
* aCalendar.setTime(oDate);
*
* int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
*
* return day2 - day1;
*/
long between_days = 0l;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
fDate = sdf.parse(sdf.format(fDate));
oDate = sdf.parse(sdf.format(oDate));
Calendar cal = Calendar.getInstance();
cal.setTime(fDate);
long time1 = cal.getTimeInMillis();
cal.setTime(oDate);
long time2 = cal.getTimeInMillis();
between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
} catch (ParseException e) {
throw new IllegalArgumentException("转换日期格式失败", e);
}

}

public static String getCurrentYmd() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(new Date());
}
}

标签:return,param,Date,日期,date,转换,Calendar,面板,calendar
From: https://www.cnblogs.com/dhdnm/p/16900982.html

相关文章

  • 日期转换-面板2
    importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;importjava.util.HashMap;importjava.util.......
  • C语言类型转换
    类型转换类型转换:在C语言中,当一个运算符的几个操作数类型不同时,编译器会在进行运算之前将他们共同转化为某种一样的数据类型,一般来说编译器会先将占用内存较小的数据转化为......
  • Go语言单词格式转换命令行工具
    学习:单词格式转换|Go语言编程之旅(eddycjy.com)代码参考:go-programming-tour-book/tour:《Go语言编程之旅:一起用Go做项目》第一章:命令行程序(Command)(github.com)......
  • C 强制类型转换获取不同数据的字节表示
    #include<stdio.h>//指向类型为unsignedchar类型的对象指针typedefunsignedchar*byte_pointer;//size_t32位4字节64位8字节voidshow_bytes(byte_pointers......
  • PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列|附
    全文下载链接:http://tecdat.cn/?p=22617本文提供了一个在统计模型中使用马可夫转换模型模型的例子,来复现Kim和Nelson(1999)中提出的一些结果。它应用了Hamilton(1989)的滤波器......
  • python字典类型与 json数据转换
    一、json是什么json是轻量级的数据交换格式,是各种编程语言互通的桥梁。是一个约定好的标准,如有人用java,有人用PHP去开发一个系统,但是这两种语言不能直接互通,那就需要先转......
  • 转换流InputStreamReader OutputStreamWriter
    【1】转换流:作用:将字节流和字符流进行转换【2】转换流属于字节流还是字符流?输入字符流InputStreamReader:字节输入流----》字符的输入流OutputStreamWriter:字符的输出流......
  • 常用进制转换
    常用进制转换当然,Java中也已经将常用的进制转换方法封装好了,我们只需要调用对应方法即可。转换 方法 返回 十进制->二进制 Integer.toBinary(intnum)......
  • 32、计算任意日期相减后的天数
    题目: 请计算任意日期相减后的天数思路:  1、定义一个函数  2、将函数中两个参数分别进行设定相应参数含义。  3、传入参数值,返回结果。结果:importdatetime......
  • Oracle plsql 计算日期间工作日天数
    declare v_fdatedate:=to_date('2013-10-4','yyyy-mm-dd'); v_ldatedate:=to_date('2013-10-22','yyyy-mm-dd'); v_interval_anumber;......