求时间差、以当前时间为判断值,获取过去、未来时间(以年、月、日、时显示),判断当前日期是周几
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
-
时间工具类
*/
public class TimeUtils {private static final String TAG = “TimeUtils”;
public static String getDateTime(String pattern) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
String dt = dateFormat.format(date);
return dt;
}/**
- 获取当前日期
- @return
*/
public static String getDate() {
return getDateTime(“yyyy-MM-dd”);
}
/**
- 获取当前时间
- @return
*/
public static String getTime() {
return getDateTime(“HH:mm:ss”);
}
/**
- 求时间差
- startTime 起始日期
- endTime 截止日期
- dateType 日期类型 HOUR/DAY/MONTH/YEAR
*/
public static int timeDifference(String startTime, String endTime, TimeEnum dateType) {
String fmtStr = null;
int nums = 0;
int timeNum = 3600 * 24;
if (dateType == TimeEnum.HOUR) {
fmtStr = “yyyy-MM-dd HH”;
timeNum = 3600;
} else if (dateType == TimeEnum.DAY) {
fmtStr = “yyyy-MM-dd”;
timeNum = 3600 * 24;
} else if (dateType == TimeEnum.MONTH) {
fmtStr = “yyyy-MM”;
timeNum = 3600 * 24 * 30;
} else if (dateType == TimeEnum.YEAR) {
fmtStr = “yyyy”;
timeNum = 3600 * 24 * 365;
} else {
fmtStr = “yyyy-MM-dd”;
}
Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(fmtStr);//此处修改日期格式
Date startDate = null;
try {
startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
startCal.setTime(startDate);
endCal.setTime(endDate);
startCal.compareTo(endCal);
//得到两个日期相差的天数
nums = ((int) (endCal.getTime().getTime() / 1000) - (int) (startCal.getTime().getTime() / 1000)) / timeNum;//此处修改日期单位
} catch (ParseException e) {
Log.e(TAG, " error msg: " + e.getMessage());
e.printStackTrace();
}
return nums;
}
/**
- 参数为负数,则获取过去时间(多少天)
- 参数为正数,则获取未来时间
- @return
*/
public static String oldDateAndTime(int oldDay) {
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, oldDay);
Date d = c.getTime();
String getoldDate = format.format(d);
return getoldDate;
}
/**
- 获取过去(n)周日期
- @return
*/
public static String getOldTimeWeek(int weekNum) {
SimpleDateFormat simFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_YEAR, weekNum);
Date newDate = calendar.getTime();
String getTime = simFormat.format(newDate);
// System.out.println("wufeng::::::>>>WEEK: " + getTime);
return getTime;
}
/**
- 获取过去n个月的日期
- @param num
- @return
*/
public static String getOldTimeMonth(int num) {
SimpleDateFormat simFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, num);
Date newDate = calendar.getTime();
String getTime = simFormat.format(newDate);
return getTime;
}
/**
- 获取过去n年的日期
- @param num
- @return
*/
public static String getOldTimeYear(int num) {
SimpleDateFormat simFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, num);
Date newDate = calendar.getTime();
String getTime = simFormat.format(newDate);
return getTime;
}
/**
- 0:星期日
- 1:星期一
- 2:星期二
- 3:星期三
- 4:星期四
- 5:星期五
- 6:星期六
*/
private static String dayNames[] = {“0”, “1”, “2”, “3”, “4”, “5”, “6”};
public static String getDayName(String date) {
String dayWeek = “”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse(date));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
dayWeek = dayNames[dayOfWeek];
System.out.print(dayWeek);
} catch (ParseException e) {
e.printStackTrace();
}
return dayWeek;
}
}
public enum TimeEnum {
HOUR,
DAY,
MONTH,
YEAR
}
------------------------------------------------如何调用--------------------------------------
String oldTimeMonth = TimeUtils.getOldTimeMonth(-2);
int timeDiffYear = TimeUtils.timeDifference(“2022-10-15 16:12:14”, “2023-03-15 15:40:12”, TimeEnum.YEAR);
int timeDiffMonth = TimeUtils.timeDifference(“2022-10-15 16:12:14”, “2023-03-15 15:40:12”, TimeEnum.MONTH);
int timeDiffDay = TimeUtils.timeDifference(“2022-10-15 16:12:14”, “2023-03-15 15:40:12”, TimeEnum.DAY);
int timeDiffHour = TimeUtils.timeDifference(“2022-10-15 16:12:14”, “2023-03-15 15:40:12”, TimeEnum.HOUR);
Log.v(TAG, “获取数据列表如下”
+ “\n” + " 过去时间: " + oldTimeMonth
+ “\n” + " 时间差: " + timeDiffYear
+ “\n” + " timeDiffMonth: " + timeDiffMonth
+ “\n” + " timeDiffDay: " + timeDiffDay
+ “\n” + " timeDiffHour: " + timeDiffHour
+ “\n” + " 过去2周日期: " + TimeUtils.getOldTimeWeek(-7 * 2));
----------------------------------------------------打印如下---------------------------------------------------------
过去时间: 2023-01-15 15:56:37
时间差: 1
timeDiffMonth: 5
timeDiffDay: 151
timeDiffHour: 3623
过去2周日期: 2023-03-01 15:56:37