一、Java时间互转工具类
public static String farmat(Date date, String fromat) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromat);
return simpleDateFormat.format(date);
}
public static String farmat(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return simpleDateFormat.format(date);
}
public static String DateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
public static String DateToStr2(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static String timeDifference(String a1, String b1) {
DateTimeFormatter forPattern = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime a2 = forPattern.parseDateTime(a1);
DateTime b2 = forPattern.parseDateTime(b1);
int months = Months.monthsBetween((ReadableInstant) b2, (ReadableInstant) a2).getMonths();
return months + "";
}
public static String randomDate(String beginDate, String endDate) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date start = simpleDateFormat.parse(beginDate);
Date end = simpleDateFormat.parse(endDate);
if (start.getTime() >= end.getTime()) {
return null;
}
long data = random(start.getTime(), end.getTime());
return DateToStr2(new Date(data));
} catch (Exception exception) {
return null;
}
}
public static long random(long begin, long end) {
long rtn = begin + (long) (Math.random() * (end - begin));
if (rtn == begin || rtn == end) {
return random(begin, end);
}
return rtn;
}
二、实现计算两个日期的月份差实例代码
1. Maven导入依赖:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.6</version>
</dependency>
2. 计算过程
package com.forezp.util;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
* 在JAVA中,如何计算两个日期的月份差?<br>
*
*
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MMdd");
DateTime start = formatter.parseDateTime("2017-07-11");
DateTime end = formatter.parseDateTime("2017-11-19");
int months = Months.monthsBetween(start, end).getMonths();
System.out.println(months);
}
}
三、java生成指定时间内的随机时间
public static void main(String[] args) {
for (int i=0;i<30;i++){
Date date = randomDate("2019-01-01","2019-01-31");
System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date));
}
}
private static Date randomDate(String beginDate,String endDate){
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse(beginDate);
Date end = format.parse(endDate);
if(start.getTime() >= end.getTime()){
return null;
}
long date = random(start.getTime(),end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static long random(long begin,long end){
long rtn = begin + (long)(Math.random() * (end - begin));
if(rtn == begin || rtn == end){
return random(begin,end);
}
return rtn;
}