常用时间格式

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 / (1000
60
60.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 );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EndNote 是一种常用的文献管理软件,可以帮助研究人员整理和引用参考文献。引用格式是使用 EndNote 时重要的一个功能,以下是常用的 EndNote 引用格式。 1. APA 格式:APA(American Psychological Association)是一种常用的学术引用格式。在 EndNote 中,可以选择 APA 格式进行引用。APA 格式包括作者的姓氏和名字的缩写、文章标题、期刊名称、页码等信息。 2. MLA 格式:MLA(Modern Language Association)是一种主要用于人文学科的引用格式。在 EndNote 中,可以选择 MLA 格式进行引用。MLA 格式包括作者的姓氏和名字、文章标题、书名、出版日期等信息。 3. Chicago 格式:Chicago(芝加哥)格式是用于人文学科和社会科学的一种常见引用格式。在 EndNote 中,可以选择 Chicago 格式进行引用。Chicago 格式包括作者的姓名、文章标题、书名、出版日期等信息。 4. Harvard 格式:Harvard 格式是一种常见的英国学术引用格式。在 EndNote 中,可以选择 Harvard 格式进行引用。Harvard 格式包括作者的姓氏和名字、文章标题、期刊名称、年份等信息。 除了上述常见的引用格式外,EndNote 还支持其他学术期刊或出版社的特定引用格式,例如 IEEE、ACS、AMA 等。用户可以根据自己研究领域和各期刊的要求选择合适的引用格式,并在 EndNote 中进行设置。在使用 EndNote 进行文献管理和引用时,可以通过导入文献、手动添加引用、自动生成参考文献列表等功能来方便地完成引用工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值