package com.st.project.common;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 格式化日期
*/
public class DateUtil {
/**
* 获取当前日期字符串
*
* @param dateFormat 日期需要的格式 eg:yyyy-MM-dd
* @return
*/
public static String getCurrentTime(String dateFormat) {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
Date currentTime = new Date();
return format.format(currentTime);
}
/**
* 将格式为dateFormat的日期字符串格式化为日期类型
*
* @param dateFormat 字符串的日期格式 eg:yyyy-MM-dd
* @param date 需要格式化的字符串
* @return
* @throws ParseException
*/
public static Date formatDate(String date, String dateFormat) throws ParseException {
if("".equals(date) || date==null){
return null;
}else {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.parse(date);
}
}
/**
* 将日期转换为dateFormat格式的字符串
*
* @param date 需要格式化的日期
* @param dateFormat 字符串的日期格式 eg:yyyy-MM-dd
* @return
*/
public static String dateToString(Date date, String dateFormat) {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.format(date);
}
/**
* 对日期进行加减操作
*
* @param date 要进行加减天数的日期
* @param addOrMinus 对日期加减天数(eg:加一天:1 减一天:-1)
* @return
* @throws ParseException
*/
public static Date dateAddOrMinus(Date date, Integer addOrMinus) {
if (addOrMinus == null || "".equals(addOrMinus)) {
addOrMinus = 0;
}
Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, +addOrMinus);
return cal.getTime();
}
/**
* 日期转换为时间戳
*
* @param date 日期
* @return
*/
public static String dateToSjc(Date date) {
if (date == null || "".equals(date)) {
return "";
} else {
return date.getTime() + "";
}
}
/**
* 时间戳转换为日期
*
* @param sjc 时间戳
* @return
*/
public static Date sjcToDate(String sjc) {
if (sjc == null || "".equals(sjc)) {
return null;
} else {
return new Date(Long.parseLong(sjc));
}
}
public static int getSecondTimestamp(Date date) {
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime());
int length = timestamp.length();
if (length > 3) {
return Integer.valueOf(timestamp.substring(0, length - 3));
} else {
return 0;
}
}
/**
* JDK自带API计算两个日期的差值 (结束时间-开始时间)
* @param startDate 开始时间
* @param endDate 结束时间
*/
public static String printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime(); //此时精确到微妙
different /= 1000; // 转成秒数
return different >=0?String.valueOf(different):"0";
}
/** * 获取指定日期是星期几
* 参数为null时表示获取当前日期是星期几
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar calendar = Calendar.getInstance();
if(date != null){
calendar.setTime(date);
}
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return weekOfDays[w];
}
/**
* 获取某个时间所处月份的天数
* @return
*/
public static int getMonthDays(Date date){
Calendar cal = java.util.Calendar.getInstance();
cal.setTime(date);
int maxDay = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
return maxDay;
}
/**
* 将某个时间所处月份的每天,用于图标展示查月份数据x轴
* @param date
* @return
*/
public static List<String> monthDayString(Date date){
List<String> days=new ArrayList<>();
int monthDays=getMonthDays(date);
for(int i=1;i<=monthDays;i++){
if(i<10){
days.add("0"+i);
}else{
days.add(i+"");
}
}
return days;
}
public static void main(String[] args) throws ParseException {
System.out.println(getRandomDouble(1.2,3.1));
}
/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 生成指定范围随机小数(保留两位)
* @param numMin 最小值
* @param numMax 最大值
* @return
*/
public static double getRandomDouble(double numMin,double numMax) {
Random rand = new Random();
int minMoney=(int)numMin;
int maxMoney=(int)numMax;
if(minMoney==maxMoney || minMoney>maxMoney){
maxMoney=minMoney+1; //最大值和最小值不能一样,最小值不能大于最大值(否则会报错)
}
DecimalFormat df = new DecimalFormat("#.00");
double doubleNum=(rand.nextInt(maxMoney-minMoney)+minMoney)+rand.nextDouble();
return Double.parseDouble(df.format(doubleNum));
}
}