package com.fms.ftl.bigScreen.utils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by xiaobai on 2019/3/5.
*
* @author xiaobai.
* @DATE 2019/3/5
*/
public class SplitDataUtil {
public static void main(String[] args) {
System.out.println("当天开始 ---- "+getDay().get("start"));
System.out.println("当天开始Str---- "+getDay().get("startStr"));
System.out.println("当天结束 ---- "+getDay().get("end"));
System.out.println("当天结束Str---- "+getDay().get("endStr"));
System.out.println("第二天开始 ---- "+getDay().get("nextStart"));
System.out.println("第二天开始Str---- "+getDay().get("nextStartStr"));
System.out.println("本周开始 ---- "+getWeek().get("start"));
System.out.println("本周开始Str---- "+getWeek().get("startStr"));
System.out.println("本周结束 ---- "+getWeek().get("end"));
System.out.println("本周结束Str---- "+getWeek().get("endStr"));
System.out.println("下周开始 ---- "+getWeek().get("nextStart"));
System.out.println("下周开始Str---- "+getWeek().get("nextStartStr"));
System.out.println("下周结束 ---- "+getWeek().get("nextEnd"));
System.out.println("下周结束Str---- "+getWeek().get("nextEndStr"));
System.out.println("本月开始 ---- "+getMonth().get("start"));
System.out.println("本月开始Str---- "+getMonth().get("startStr"));
System.out.println("本月结束 ---- "+getMonth().get("end"));
System.out.println("本月结束Str---- "+getMonth().get("endStr"));
System.out.println("下个月开始 ---- "+getMonth().get("nextStart"));
System.out.println("下个月开始Str---- "+getMonth().get("nextStartStr"));
}
/**
* 时间段截取拼接
*
* @param attributeName
* @param attribute
* @return
*/
public static Map<String, String> splitData(String attributeName, String attribute) {
String[] strings = attribute.split("~");
StringBuilder stringBuilder = new StringBuilder();
Map<String, String> map = new HashMap<>();
for (int j = 1; j <= 2; j++) {
for (String string : strings) {
for (int i = 1; strings.length <= 2; i++) {
if (j == 1) {
stringBuilder.append(attributeName + ">=" + string);
map.put("start", string);
} else if (j == 2) {
stringBuilder.append("AND " + attributeName + "<=" + string);
map.put("end", string);
}
j++;
break;
}
}
break;
}
String strbdr = stringBuilder.toString();
map.put("timeQuantum", strbdr);
return map;
}
/**
* 转换结束时间格式
* @param endDate
* @return
*/
public static String changeEndDate(String endDate) {
long dataToLong = dataToLong(endDate);
long zero = dataToLong / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
//前一天23点59分59秒
long twelve = zero + 24 * 60 * 60 * 1000 - 1;
//转换为今天23点59分59秒
Date date = new Date(twelve);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, 1);
Date time = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String toEndDate = sdf.format(time);
return toEndDate;
}
/**
* 转换为LONG类型时间
*
* @param startData
* @return
*/
public static Long dataToLong(String startData) {
SimpleDateFormat sdf;
if (startData.length() > 10) {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
startData += " 00:00:00";
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
Long c = null;
try {
c = Long.valueOf(sdf.parse(startData).getTime());
} catch (ParseException e1) {
e1.printStackTrace();
}
return c;
}
/**
* Long类型转String
* @param time
* @return
*/
public static String LongToString(Long time){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startStr = sdf.format(time);
return startStr;
}
/**
* 获取时间差
*
* @param startDate
* @param endDate
* @return
*/
public static Integer getTimeDifference(long startDate, long endDate) {
long longDays = (endDate - startDate) / (24 * 60 * 60 * 1000);
int days = (int) longDays;
return days;
}
/**
* 获取当天时间
*
* @return
*/
public static Map<String,Object> getDay() {
Map<String,Object> day = new HashMap<>();
day.put("start",getBeginDay().getTime());
day.put("startStr",LongToString(getBeginDay().getTime()));
day.put("end",getFinishDay().getTime());
day.put("endStr",LongToString(getFinishDay().getTime()));
day.put("nextStart",getNextBeginDay().getTime());
day.put("nextStartStr",LongToString(getNextBeginDay().getTime()));
return day;
}
/**
* 获取当天开始时间
* @return
*/
private static Date getBeginDay(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date zero = calendar.getTime();
return zero;
}
/**
* 获取当天结束时间
* @return
*/
private static Date getFinishDay(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 获取明天开始时间
* @return
*/
private static Date getNextBeginDay(){
Calendar cal = new GregorianCalendar();
cal.setTime(getBeginDay());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
/**
* 获取本周的开始和结束时间
* @return
*/
public static Map<String, Object> getWeek() {
Map<String, Object> week = new HashMap<>();
week.put("start", getBeginWeek().getTime());
week.put("startStr", getBeginWeek());
week.put("end",getFinishWeek().getTime());
week.put("endStr",getFinishWeek());
week.put("nextStart",getNextBeginWeek().getTime());
week.put("nextStartStr",getNextBeginWeek());
week.put("nextEnd",getNextFinishWeek().getTime());
week.put("nextEndStr",getNextFinishWeek());
return week;
}
/**
* 获取本月的开始和结束时间
* @return
*/
public static Map<String,Object> getMonth(){
Map<String, Object> month = new HashMap<>();
month.put("start",getBeginMonth().getTime());
month.put("startStr",getBeginMonth());
month.put("end",getFinishMonth().getTime());
month.put("endStr",getFinishMonth());
month.put("nextStart",getNextBeginMonth().getTime());
month.put("nextStartStr",getNextBeginMonth());
return month;
}
/**
* 获取本月开始时间
* @return
*/
private static Date getBeginMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
/**
* 获取下个月开始时间
* @return
*/
private static Date getNextBeginMonth() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return getDayStartTime(calendar.getTime());
}
/**
* 获取本月结束时间
* @return
*/
private static Date getFinishMonth(){
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
/**
* 获取本周开始时间
* @return
*/
private static Date getBeginWeek(){
Date date = new Date();
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
calendar.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(calendar.getTime());
}
/**
* 获取本周结束时间
* @return
*/
private static Date getFinishWeek(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(getBeginWeek());
calendar.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = calendar.getTime();
return getDayEndTime(weekEndSta);
}
/**
* 获取下周开始时间
* @return
*/
private static Date getNextBeginWeek(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(getBeginWeek());
calendar.add(Calendar.DAY_OF_WEEK, 7);
Date weekEndSta = calendar.getTime();
return getDayStartTime(weekEndSta);
}
/**
* 获取下周结束时间
* @return
*/
private static Date getNextFinishWeek(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(getFinishWeek());
calendar.add(Calendar.DAY_OF_WEEK, 7);
Date weekEndSta = calendar.getTime();
return getDayEndTime(weekEndSta);
}
/**
* 获取某个时间的结束时间
* @param time
* @return
*/
private static Timestamp getDayEndTime(Date time) {
Calendar calendar = Calendar.getInstance();
if (time != null) {
calendar.setTime(time);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
/**
* 获取某个时间的开始时间
* @param time
* @return
*/
private static Timestamp getDayStartTime(Date time) {
Calendar calendar = Calendar.getInstance();
if (time != null) {
calendar.setTime(time);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
/**
* 获取本年是哪一年
* @return
*/
private static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
/**
* 获取本月是哪月
* @return
*/
private static Integer getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
}