/**
* 返回当前日期的16进制
*/
public static String getDateHex ( ) {
StringBuffer sb = new StringBuffer( ) ;
Date date = new Date( ) ;
SimpleDateFormat formatter = new SimpleDateFormat( "yy-MM-dd-HH-mm-ss" ) ;
String nowDate[ ] = formatter.format( date) .split( "-" ) ;
for ( int i = 0 ; i < nowDate.length; i++) {
int da = Integer.parseInt( nowDate[ i] ) ;
String hex = Integer.toHexString( da & 0xFF) ;
if ( hex.length( ) == 1 ) {
hex = '0' + hex;
}
sb.append( hex) ;
}
return sb.toString( ) ;
}
/**
* 获得两个时间段之内的所有日期小时:例如传参数:"2018-12-06 01" 和"2018-12-06 23" , 返回结果:[ 2018 -12-06 01,
* 2018 -12-06 02, 2018 -12-06 03, 2018 -12-06 04, .. .. .. , 2018 -12-06 23 ]
*
* @param beginDate
* @param endDate
* @return
* @throws ParseException
* @throws java.text.ParseException
*/
public static List< String> getHoursBetweenTwoDate( String beginDate, String endDate)
throws ParseException, java.text.ParseException {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH" ) ;
List< String> lDate = new ArrayList< String> ( ) ;
lDate.add( beginDate) ; // 把开始时间加入集合
Calendar cal = Calendar.getInstance( ) ;
// 使用给定的 Date 设置此 Calendar 的时间
cal.setTime( sdf.parse( beginDate)) ;
boolean bContinue = true ;
while ( bContinue) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cal.add( Calendar.HOUR, 1 ) ;
// 测试此日期是否在指定日期之后
if ( sdf.parse( endDate) .after( cal.getTime( )) ) {
lDate.add( sdf.format( cal.getTime( )) ) ;
} else {
break ;
}
}
lDate.add( endDate) ; // 把结束时间加入集合
return lDate;
}
/**
* 判断开始时间,是否再结束时间之后,是,返回true; 否返回false
*
* @param startTime:开始时间;endTime:结束时间
* @return true or false
*/
public static boolean compareDates( String startTime, String endTime) {
try {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
Date date1 = sdf.parse( startTime) ;
Date date2 = sdf.parse( endTime) ;
if ( date1.after( date2)) {
return true ;
}
} catch ( ParseException ex) {
ex.printStackTrace( ) ;
}
return false ;
}
//获取固定分钟后的时间
public static String getFiveMinAfter( String time, int minute) {
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = null;
try {
date = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) .parse( time) ;
} catch ( ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace( ) ;
}
Calendar now = Calendar.getInstance( ) ;
now.setTime( date) ;
now.add( Calendar.MINUTE, minute) ;
Date afterFiveMin = now.getTime( ) ;
return format.format( afterFiveMin) ;
}
/**
* 判断时间是否整点
*
* @return
*/
public static boolean isHourTime( String date ) {
SimpleDateFormat sdf1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
Date date1 = null;
try {
date1 = sdf1.parse( date) ;
} catch ( ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace( ) ;
}
// 判断是否整点
// 格式化时间串,HH:MM:SS分别是时分秒,这里只判断分钟,有mm就够了
// 需要秒的话,就mm:ss,下面就判断是否 00:00
SimpleDateFormat sdf = new SimpleDateFormat( "mm" ) ;
String timestr = sdf.format( date1) ;
// 判断是否为需要的时间,比如整点时分钟为00
if ( timestr.equals( "00" )) {
return true ;
} else {
return false ;
}
}
/**
* 获取固定天数后的时间
* @param date
* @param dayNum
* @return
*/
public static String getDateNum( Date date,int dayNum) {
//对当前时间进行转换 括号里面的是转换规则( "yyyy-MM-dd HH:mm:ss" )
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ) ;
date.setTime( System.currentTimeMillis( )) ;
Calendar c = Calendar.getInstance( ) ;
c.setTime( date) ;
//5是传的参数 就是说要获取几天后的当前具体时间
c.add( Calendar.DATE, dayNum) ;
Date d3 = c.getTime( ) ;
String date3 = format.format( d3) ;
return date3;
}
/**
* 将字符串,转化为时间
*
* @param time
* @return
*/
public static Date changeDateByString( String time ) {
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = null;
try {
date = format.parse( time) ;
} catch ( ParseException e) {
e.printStackTrace( ) ;
}
return date ;
}
/**
* 判断当前日期所属区间范围;
* @param beginTime :开始时间
* @param endTime:结束时间
* @return 0:开始时间之前;1:开始时间与结束时间之间;2:结束时间之后;999;转化出错
*/
public static int isBetween(LocalDateTime beginTime, LocalDateTime endTime) {
//0:开始时间之前;1:开始时间与结束时间之间;2:结束时间之后;999;转化出错
int flag = 999;
LocalDateTime now = LocalDateTime.now();
if(now.isBefore(beginTime)){
flag = 0;
}else if (now.isAfter(beginTime) && now.isBefore(endTime)) {
flag = 1;
}else if (now.isAfter(endTime)){
flag = 2;
}
return flag;
}
/**
* 获取上个月的第一天
* @return
*/
public static String getLastMonth ( ) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ) ;
Calendar calendar = Calendar.getInstance( ) ;
// 调整到上个月
calendar.add( Calendar.MONTH, -1) ;
// 设置为该月的第一天
calendar.set( Calendar.DAY_OF_MONTH, 1 ) ;
Date firstDayOfLastMonth = calendar.getTime( ) ;
String firstDayOfLastMonthStr = sdf.format( firstDayOfLastMonth) ;
return firstDayOfLastMonthStr;
}
/**
* 时间戳转日期
* @param timestamp:时间戳
* @return
*/
public static String stampToDate( long timestamp) {
Date date = new Date( timestamp) ;
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
String formattedDate = dateFormat.format( date) ;
System.out.println( "时间戳转日期: " + formattedDate) ;
return formattedDate;
}
/**
* 日期转时间戳
* @param date
* @return
*/
public static Long dateToStamp( String date ) {
String strDate = "2023-04-05 10:20:30" ;
SimpleDateFormat parser = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
try {
Date parsedDate = parser.parse( strDate) ;
long parsedTimestamp = parsedDate.getTime( ) ;
return parsedTimestamp;
} catch ( Exception e) {
e.printStackTrace( ) ;
}
return null;
}
/**
* 日期转字符串
* @param date
* @return
*/
public static String dateToStr( Date date ) {
SimpleDateFormat parser = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
return parser.format( date) ;
}
/**
* 字符串转日期
* @param date
* @return
*/
public static Date strToDate( String date ) {
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
try {
return formatter.parse( date) ;
} catch ( ParseException e) {
e.printStackTrace( ) ;
}
return null;
}
/**
* 秒/毫秒转字符串日期
* @param second
* @return
*/
public static String secondToDate( Long second) {
String strSecond = Long.toString( second) ;
LocalDateTime dateTime = null;
//10位的是秒
if( strSecond.length( ) == 10 ) {
dateTime = LocalDateTime.ofEpochSecond( second, 0 , ZoneOffset.of( "+8" )) ;
}
//13位的是毫秒
if( strSecond.length( ) == 13 ) {
dateTime = new Date( second) .toInstant( ) .atOffset( ZoneOffset.of( "+8" )) .toLocalDateTime( ) ;
}
StringBuffer sb = new StringBuffer( ) ;
sb.append( dateTime.getYear( )) ;
sb.append( "-" ) ;
sb.append( dateTime.getMonth( ) .getValue( )) ;
sb.append( "-" ) ;
sb.append( dateTime.getDayOfMonth( )) ;
sb.append( " " ) ;
sb.append( dateTime.getHour( )) ;
sb.append( ":" ) ;
sb.append( dateTime.getMinute( )) ;
sb.append( ":" ) ;
sb.append( dateTime.getSecond( )) ;
return sb.toString( ) ;
}
//判断固定日期,是否在两个日志之间
public static void main( String[ ] args) {
// 固定日期
LocalDate fixedDate = LocalDate.of( 2023 , 4 , 15 ) ;
// 两个日期
LocalDate date1 = LocalDate.of( 2023 , 4 , 1 ) ;
LocalDate date2 = LocalDate.of( 2023 , 4 , 30 ) ;
// 判断固定日期是否在date1和date2之间
boolean isBetween = fixedDate.isAfter( date1) && fixedDate.isBefore( date2) ;
System.out.println( "固定日期是否在date1和date2之间: " + isBetween) ;
}
/**
* 获取两个日期直接所有的天数
* @param startDate:开始日期 "2023-01-01"
* @param endDate:结束日期"2023-01-05"
* @return
*/
public static List< LocalDate> getDatesBetween( String startDate, String endDate) {
LocalDate start = LocalDate.parse( startDate) ;
LocalDate end = LocalDate.parse( endDate) ;
Period period = Period.between( start, end) ;
int days = period.getDays( ) ;
int daysBetween = ( int) Math.signum( days) * ( Math.abs( days) + 1 ) ;
List< LocalDate> dates = new ArrayList<> ( daysBetween) ;
for ( int i = 0 ; i < daysBetween; i++) {
dates.add( start.plusDays( i)) ;
}
return dates;
}
/**
* 获取两个日期直接所有的天数
* @param begin:开始日期"2023-01-01 00:00:00"
* @param end:结束日期"2023-01-05 23:59:59"
* @return
*/
public static List< String> getAllDateList( String begin,String end) {
List< String> resList = new ArrayList< String> ( ) ;
Date startDate = new Date( ) ;
Date endDate = new Date( ) ;
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ) ;
try {
startDate = format.parse( begin) ;
endDate = format.parse( end) ;
} catch ( ParseException e) {
e.printStackTrace( ) ;
}
List< Date> dates = new ArrayList<> ( ) ;
Calendar calendar = Calendar.getInstance( ) ;
calendar.setTime( startDate) ;
while ( calendar.getTime( ) .before( endDate)) {
dates.add( calendar.getTime( )) ;
calendar.add( Calendar.DATE, 1 ) ;
}
for ( int i = 0 ; i < dates.size( ) ; i++) {
Date date = dates.get( i) ;
String dateStr = dateToStrNew( date) ;
resList.add( dateStr) ;
}
return resList;
}