1、mysql更新一段时间内的随机时间
UPDATE devicestatus SET statustime = concat(‘2020-05-0’, floor(4+rand()*6),’ ‘, floor(10+rand()*10),’:’,floor(10+rand()49),’:’,floor(10+rand()49));
2、 判断两个timestamp类型的大小 第二个数比第一个大 返回 true 第二个数比第一个数小 返回 false
public static Boolean compareTimeStamp(Timestamp timestamp , Timestamp timestamp2) {
Long time1 = 0l;
Long time2 = 0l;
if(timestamp != null ) {
time1 = timestamp.getTime();
}
if(timestamp2 != null) {
time2 = timestamp2.getTime();
}
if(timestamp == null) {
return true;
}
if(time1 > time2) {
return false;
}else {
return true;
}
}
3、/获取指定年月的下月
public static String getNextMonth(String string) {
try
{
SimpleDateFormat sdf = new SimpleDateFormat (“yyyy-MM”);
Date date = sdf.parse (string);
Calendar calendar = Calendar.getInstance ();
calendar.setTime (date);
calendar.add (Calendar.MONTH, 1);
return sdf.format (calendar.getTime ());
}
catch (ParseException e)
{
e.printStackTrace ();
}
return “”;
}
4、获取指定时间的上个月
public static String getbeforeMonth(String string) {
try
{
SimpleDateFormat sdf = new SimpleDateFormat (“yyyy-MM”);
Date date = sdf.parse (string);
Calendar calendar = Calendar.getInstance ();
calendar.setTime (date);
calendar.add (Calendar.MONTH, -1);
return sdf.format (calendar.getTime ());
}
catch (ParseException e)
{
e.printStackTrace ();
}
return “”;
}
5、指定年月日的上个月
public static String getbeforeMonth1(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -1);
return simpleDateFormat.format(calendar.getTime());
}
6、指定日期的后几天
public static String getDateAfter(String d,int day){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
Date date;
try {
date = simpleDateFormat.parse(d);
Calendar now =Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
return simpleDateFormat.format(now.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
7、获取两个日期的差值
public static String getTimeStampRes(Timestamp timestamp1 , Timestamp timestamp2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
String date1 = simpleDateFormat.format(timestamp1);
String date2 = simpleDateFormat.format(timestamp2);
date1 = date1 + “:00”;
date2 = date2 + “:00”;
timestamp1 = Timestamp.valueOf(date1);
timestamp2 = Timestamp.valueOf(date2);
Long res= timestamp2.getTime() - timestamp1.getTime();
Double double1 = res / (10006060.0);
DecimalFormat decimalFormat = new DecimalFormat("#####################.00");
return decimalFormat.format(double1);
}
8、获取两个日期的差值
public static Double getTimeStampRes1(String timestamp1 , String timestamp2) {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
// String date1 = simpleDateFormat.format(timestamp1);
// String date2 = simpleDateFormat.format(timestamp2);
String date1 = timestamp1 + " 24:00:00";
String date2 = timestamp2 + " 00:00:00";
Timestamp one = Timestamp.valueOf(date1);
Timestamp two = Timestamp.valueOf(date2);
Long res= one.getTime() - two.getTime();
Double double1 = res / (10006060.0*24);
return double1;
}
9、获取明天的时间
public static String getTomorrow(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);
String dateString = formatter.format(date);
return dateString;
}
/**
* 获取明天的时间
* @param stringDate : 前一天时间
* @return
*/
public static String getTomorrow(String stringDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = formatter.parse(stringDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
String dateString = formatter.format(date);
return dateString;
}
10、JAVA获取某段时间内的所有日期
public static List < Date > findDates(Date dBegin, Date dEnd) {
List lDate = new ArrayList();
lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}
return lDate;
}
11、获取某月的最后一天
public static String getLastDayOfMonth(int year,int month)
{
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
12、获取指定年月日的后几个月的时间
public static String getAfterDateMonth(String time , int month) {
Calendar c = Calendar.getInstance();//获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try{
date = sdf.parse(time);//初始日期
}catch(Exception e){
}
c.setTime(date);//设置日历时间
c.add(Calendar.MONTH,month);//在日历的月份上增加6个月
return sdf.format(c.getTime());
}
13、昨天的时间
public static String getYesterday(String time) {
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
14、某月最后一天
public static String getMonthEnd(int year,int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(“Asia/Shanghai”));
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = Date.from(zonedDateTime.toInstant());
return sdf.format(date );
}