Skip to content

Instantly share code, notes, and snippets.

@logicjwell
Created April 28, 2019 07:50
Show Gist options
  • Save logicjwell/dcb83f16a76672302a0aad5b49f862cc to your computer and use it in GitHub Desktop.
Save logicjwell/dcb83f16a76672302a0aad5b49f862cc to your computer and use it in GitHub Desktop.
[DateTime工具类] #datetime #工具类
package com.huacai.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by Yu Qun on 2017/6/21.
*/
@Component
@Slf4j
public class DateTimeUtil {
private static final Calendar CALENDAR = Calendar.getInstance(Locale.CHINA);
/**
* 从当前时间开始minutes分钟后(minutes是正值)/前(minutes是否负值)
*
* @param minutes
* @return
*/
public Date dateFromMinutes(int minutes) {
Date currDate = CALENDAR.getTime();
Date newDate = DateUtils.addMinutes(currDate, minutes);
return newDate;
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyyMMdd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 将短时间格式时间转换为字符串 yyyy年MM月dd日
*
* @param dateDate
* @return
*/
public String dateToStr(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* date2比date1多的天数
* @param date1
* @param date2
* @return
*/
public static int differentDays(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
//同一年
if (year1 != year2)
{
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
//闰年
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
timeDistance += 366;
} else //不是闰年
{
timeDistance += 365;
}
}
return timeDistance + (day2 - day1);
} else //不同年
{
return day2 - day1;
}
}
/**
* 日期格式字符串转换成时间戳
* @param date_str 字符串日期
* @param format 如:yyyy-MM-dd HH:mm:ss
* @return
*/
public static Long date2TimeStamp(String date_str,String format){
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(date_str).getTime()/1000;
} catch (Exception e) {
log.error(e.toString());
}
return 0L;
}
/**
* 将短时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToStrs(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式时间转换为字符串
*
* @param dateDate
* @return
*/
public String dateFromDateToDate(Date dateDate,String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String dateString = formatter.format(dateDate);
return dateString;
}
public static String dateFromDateByPattern(Date dateDate,String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 字符串转时间
*
* @param dateStr
* 时间格式的字符串
* @param pattern
* 字符串的时间格式
* @return 格式化后的时间
* @throws ParseException
*/
public static Date strToDate(String dateStr, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date parse = null;
try {
parse = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/**
* 获取给定时间延后或前移几天的时间,delay为前移或后延的天数
*
* @return
*/
public static Date getBeforAndAfterDate(Date date, int delay) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, delay);
date = calendar.getTime();
return date;
}
/**
* 获取给定时间延后或前移几天的时间,delay为前移或后延的天数
*
* @return
*/
public static Date getBeforAndAfterDate(Date date, int delay, String timeFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
//Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, delay);
date = calendar.getTime();
return date;
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static Date strToDateLong(String strDate, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public Map<String, Object> commonDate(String startTime, String endTime) {
Map<String, Object> map = new HashMap<>();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
if (!StringUtils.isEmpty(startTime)) {
try {
startDate = format1.parse(startTime.toString());
if (!StringUtils.isEmpty(endTime)) {
endDate = DateTimeUtil.getBeforAndAfterDate(format1.parse(endTime.toString()), 1);
} else {
endDate = new Date();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
map.put("startDate", startDate);
map.put("endDate", endDate);
return map;
}
public static void main(String[] args){
/* String str = "(降价提醒)尊敬的用户,有一条货源(秘鲁鱿鱼 500-1000)降价啦!打开华采找鱼搜索“秘鲁大片”查看详情#code#。(下载华采找鱼App,随时随地查看货源!)回T退订";
String code = str.replace("#code#", "111");
System.out.println(code);
Integer integer = new Integer("010");
System.out.println(integer);*/
for(int a =0; a<5; a++){
double random = Math.random();
int b = (int)(random * 900000) +100000;
System.out.println(b);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment