标签:return,Long,date,日期,new,Date,转换,Calendar,面板 From: https://www.cnblogs.com/dhdnm/p/16900989.html
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
public class DatapanelUtil
{
/**
* 当前时间戳 所在的年周
* @param data
* @return 20191 201923
* @see [类、类#方法、类#成员]
*/
public static Long getCurrentWeek(Long data)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date(data));
c.add(3, 0 * -1);
Date d = c.getTime();
c = Calendar.getInstance();
c.setTime(d);
return Long.parseLong(c.get(Calendar.YEAR) + "" + c.get(Calendar.WEEK_OF_YEAR));
}
/**
* 初始化统计数据
* @param indoorSubRunReq
* @see [类、类#方法、类#成员]
*/
public static Map<String, Integer> getBaseTimeData(Long finishTime)
throws Exception
{
Map<String, Integer> paramMap = new HashMap<String, Integer>();
String finishTempTime = stampToDate(finishTime);
String[] timeArray = finishTempTime.substring(0, 10).split("-");
paramMap.put("year", Integer.parseInt(timeArray[0]));
paramMap.put("month", Integer.parseInt(timeArray[1]));
paramMap.put("day", Integer.parseInt(timeArray[2]));
return paramMap;
}
/**
* 时间戳转换为时间
* @param s
* @return
* @see [类、类#方法、类#成员]
*/
private static String stampToDate(Long s)
{
String res;
if (String.valueOf(s).length() == 10)
{
s *= 10;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(s);
res = simpleDateFormat.format(date);
return res;
}
/**
* string转换时间
* @param dateString
* @return
* @throws Exception
* @see [类、类#方法、类#成员]
*/
private static Date stringToDate(String dateString)
throws Exception
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
return date;
}
/**
* 获取时间所在周
* <一句话功能简述>
* <功能详细描述>
* @param finishDate
* @return
* @see [类、类#方法、类#成员]
*/
public static String getWeekByTime(Date finishDate)
{
Calendar c = Calendar.getInstance();
c.setTime(finishDate);
SimpleDateFormat sdf = new SimpleDateFormat("ww");
if (c.get(Calendar.DAY_OF_WEEK) == 1)
{
c.add(Calendar.WEEK_OF_YEAR, -2);
}
else
{
c.add(Calendar.WEEK_OF_YEAR, -1);
}
return sdf.format(c.getTime());
}
/**
*
* 当前时间,周第一天和最后一天
* 对应的日期
* @param falg
* true:第一天 ;false:最后一天
* @return date
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public static Long getDayNum(boolean falg, Long dateTime)
throws Exception
{
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
if (falg)
{
c.setTime(new Date(dateTime));
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 获取本周一的日期
}
else
{
c.setTime(new Date(dateTime + 7 * 24 * 60 * 60 * 1000));
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 获取本周日的日期
}
String finishTempTime = stampToDate(c.getTime().getTime());
String[] timeArray = finishTempTime.substring(0, 10).split("-");
// return c.getTime().getTime();
return Long.parseLong(timeArray[2]);
}
/**
*
* 根据当前时间计算每周的天数 <功能详细描述>
*
* @param date
* @param num
* 本周为0 上周为1 依次类推
* @return
* @see [类、类#方法、类#成员]
*/
public static String getWeekDay(Date date, Integer num)
{
num = num == null ? 0 : num;
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
c.add(Calendar.DAY_OF_MONTH, num);
int mt = (c.get(Calendar.MONTH) + 1);
int dt = c.get(Calendar.DAY_OF_MONTH);
return c.get(Calendar.YEAR) + "" + (mt <= 9 ? ("0" + mt) : (mt + "")) + "" + (dt <= 9 ? ("0" + dt) : (dt + ""));
}
/**
* 获取日期当前月初,月末YMD
* @param b true 月初 false 月末
* @param dateTime
* @return
* @see [类、类#方法、类#成员]
*/
public static Long getYMDMonth(Boolean b, Long dateTime)
{
// 获取当月第一天和最后一天
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Long ymd;
Calendar cale = Calendar.getInstance();
cale.setTime(new Date(dateTime));
if (b)
{
// 获取前月的第一天
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
ymd = Long.parseLong(format.format(cale.getTime()));
}
else
{
// 获取前月的最后一天
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
ymd = Long.parseLong(format.format(cale.getTime()));
}
return ymd;
}
/**
* 是否为7天外数据
*
* @param dateTime 13位时间戳
* @return
*/
public static boolean isNotTimeThenSeven(Long dateTime)
{
int temp = 7 * 24 * 60 * 60 * 1000;
if (dateTime + temp > System.currentTimeMillis())
{
return false;
}
return true;
}
/**
* @Title: getYmdBeforeToday
* @Description: 获取n天前的ymd时间
* @return: Long
*/
public static Long getYmdBeforeToday(int n)
{
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
// 把日期推前一天.
calendar.add(Calendar.DATE, - n);
date = calendar.getTime();
return Long.valueOf(formater.format(date));
}
}