package com.helloBike.data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
public class Data {
public static void main(String[] args) throws ParseException {
//LocalDate localDate = LocalDate.parse("2017-10-07");
//LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
//System.out.println(localDate);
//System.out.println(nextSunday);
//a(20171004);
//System.out.println("****************************************");
getFirstDay("2017-10-17");
System.out.println("****************************************");
getLastDay1("2017-10-17");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String a="2017-10-17";// new Date()为获取当前系统时间
Date b =df.parse(a);
System.out.println("Date a="+b);
if(true==identyWeek(b)){
System.out.println("1");
}else{
System.out.println("0");
}
//stampToDate("20171004");
//LocalDate c = LocalDate.now();
//System.out.println("LocalDate="+c);
//localToDate(c);
//System.out.println("localToDate(c)="+localToDate(c));
//System.out.println("****************************************");
}
/**
* 获取当前时间并和传入的时间判断,当传入时间的最后一天小于这一天的最后一天的时候表示这周没过完就不执行语句
* @param data
* @return
* @throws ParseException
*/
public static boolean identyWeek(Date data) throws ParseException{
//获取传入时间的最后一天
SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("identyWeekdate1="+date1);
String date2 = date1.format(data);
System.out.println("identyWeek date2="+date2);
System.out.println("getLastDay date2");
String lastTime = getLastDay1(date2);
System.out.println("identyWeek lastTime="+lastTime);
//获取当前时间
Date date= new Date();//创建一个时间对象,获取到当前的时间
System.out.println("当前时间date"+date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置时间显示格式
String str = sdf.format(date);//将当前时间格式化为需要的类型
System.out.println("identyWeek str="+str);
System.out.println("getLastDay1 ");
String nowLastTime = getLastDay1(str);
System.out.println("nowLastTime="+nowLastTime);
//当当前时间最后一天和开始时间最后一天
int res=lastTime.compareTo(nowLastTime);
if(res<0)
return true;
else
return false;
}
/**
* 传入字符串类型日期,返回该日期的本周最后天(这里设置的是周5为最后一天)
* getLastDay
* @param x
* @throws ParseException
*/
public static String getLastDay1(String x) throws ParseException{
Date InputDate = new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(x));
//创建一个日历对象cDate
Calendar cDate = Calendar.getInstance();
//调用日历对象的方法得到本周第一天的值
cDate.setFirstDayOfWeek(Calendar.MONDAY);
//将时间传给日历对象
cDate.setTime(InputDate);
//创建一个日历对象firstDate
Calendar lastDate = Calendar.getInstance();
//设置一周第一天日期参数 ,这里设置为monday 星期一 ,周一到周日
lastDate.setFirstDayOfWeek(Calendar.MONDAY);
//设置日历时间,当前时间为输入的时间
lastDate.setTime(InputDate);
//设置获得的年份相对于第一年的,。没什么用
if(cDate.get(Calendar.WEEK_OF_YEAR)==1&&cDate.get(Calendar.MONTH)==11){
lastDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR)+1);
}
//得到这一点相对于一周来说是星期几,星期2的返回值是3
int week = cDate.get(Calendar.DAY_OF_WEEK)-1;
System.out.println("week="+week);
//判断这周是否为周6周日,是的话就算出下一周的起始时间,然后推算出周六的时间,
if(week==0|| week==6){
System.out.println("在周日或周六的时候");
System.out.println("x="+x);
//得到本年的周数
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR)+1;
//设置本年的周数
lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY+6);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String lastTime = new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime());
return lastTime;
}else {
//if(week==1||week==2||week==3||week==4||week==5)
System.out.println("不再周六或周日");
System.out.println("x="+x);
//当本周不为周六和周日的时候
System.out.println("挡在周一或周六的时候");
//得到本年的周数-1
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);
System.out.println("所在第几周:"+typeNum);
//设置本年的周数
lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY+6);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String lastTime = new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime());
return lastTime;
}
}
/**
*获得系统当天的时间,将localdate格式转换成date格式的日期
*
*/
public static Date localToDate(LocalDate a) throws ParseException{
//设置转换格式.时间偏移id
ZoneId zone = ZoneId.systemDefault();
//使用ZonedDateTime将LocalDate转换为Instant。
Instant instant = a.atStartOfDay().atZone(zone).toInstant();
//使用from()方法从Instant对象获取Date的实例
java.util.Date date = Date.from(instant);
//将data按照一定的格式来分辨.
String b = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
System.out.println("b="+b);
Date c =new SimpleDateFormat("yyyy-MM-dd").parse(b);
System.out.println("c="+c);
return c;
}
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
System.out.println("res="+res);
return res;
}
/**
* 传入一个string类型的参数返回该周的最后一天(这里设置最后一天为周5)
* get last day
*/
public static void getLastDay(String x) throws ParseException{
Date InputDate = new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(x));
//创建一个日历对象cDate
Calendar cDate = Calendar.getInstance();
//调用日历对象的方法得到本周第一天的值
cDate.setFirstDayOfWeek(Calendar.MONDAY);
//将时间传给日历对象
cDate.setTime(InputDate);
//创建一个日历对象firstDate
Calendar lastDate = Calendar.getInstance();
//设置一周第一天日期参数 ,这里设置为monday 星期一 ,周一到周日
lastDate.setFirstDayOfWeek(Calendar.MONDAY);
//设置日历时间,当前时间为输入的时间
lastDate.setTime(InputDate);
//设置获得的年份相对于第一年的,。没什么用
if(cDate.get(Calendar.WEEK_OF_YEAR)==1&&cDate.get(Calendar.MONTH)==11){
lastDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR)+1);
}
//得到这一点相对于一周来说是星期几,星期2的返回值是3
int week = cDate.get(Calendar.DAY_OF_WEEK);
System.out.println("week="+week);
//判断这周是否为周6周日,是的话就算出下一周的起始时间,然后推算出周六的时间,
if(week==0|| week==6){
System.out.println("在周一或周六的时候");
System.out.println("x="+x);
//得到本年的周数
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR)+1;
//设置本年的周数
lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY+6);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String firstTime = new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime());
System.out.println("firstTime:"+firstTime);
}else{
System.out.println("不再周六或周日");
System.out.println("x="+x);
//当本周不为周六和周日的时候
System.out.println("挡在周一或周六的时候");
//得到本年的周数-1
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);
System.out.println("所在第几周:"+typeNum);
//设置本年的周数
lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY+6);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String lastTime = new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime());
System.out.println("firstTime:"+lastTime);
}
}
/**
*
* 传入一个时间参数,返回该日期的周六
* @param x
* @throws ParseException
*/
public static void getFirstDay(String x) throws ParseException{
Date InputDate = new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(x));
//创建一个日历对象cDate
Calendar cDate = Calendar.getInstance();
//调用日历对象的方法得到本周第一天的值
cDate.setFirstDayOfWeek(Calendar.MONDAY);
//将时间传给日历对象
cDate.setTime(InputDate);
//创建一个日历对象firstDate
Calendar firstDate = Calendar.getInstance();
//设置一周第一天日期参数 ,这里设置为monday 星期一 ,周一到周日
firstDate.setFirstDayOfWeek(Calendar.MONDAY);
//设置日历时间,当前时间为输入的时间
firstDate.setTime(InputDate);
//设置获得的年份相对于第一年的,。没什么用
if(cDate.get(Calendar.WEEK_OF_YEAR)==1&&cDate.get(Calendar.MONTH)==11){
firstDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR)+1);
}
//得到这一点相对于一周来说是星期几,星期2的返回值是3
int week = cDate.get(Calendar.DAY_OF_WEEK)-1;
System.out.println("week="+week);
//判断这周是否为周6周日,是的话就算出下一周的起始时间,然后推算出周六的时间,
if(week==0|| week==6){
System.out.println("挡在周一或周六的时候");
System.out.println("x="+x);
//得到本年的周数
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);
//设置本年的周数
firstDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
firstDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String firstTime = new SimpleDateFormat("yyyy-MM-dd").format(firstDate.getTime());
System.out.println("firstTime:"+firstTime);
}else{
System.out.println("不再周六或周日");
System.out.println("x="+x);
//当本周不为周六和周日的时候
System.out.println("挡在周一或周六的时候");
//得到本年的周数-1
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR)-1;
System.out.println("所在第几周:"+typeNum);
//设置本年的周数
firstDate.set(Calendar.WEEK_OF_YEAR, typeNum);
//设置本周的天数,本周定位为SATURDAY
firstDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
//设置将本周周日设置为第一天
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
//所在周的起始日期
String firstTime = new SimpleDateFormat("yyyy-MM-dd").format(firstDate.getTime());
System.out.println("firstTime:"+firstTime);
}
}
/**
*传入一个int类型的时间参数,返回该天属于第几周,星期几,本周开始时间,本周结束时间(周一为开始时间,周日为结束时间)
**/
public static void a(int x) throws ParseException{
//新建一个日期类型,
Date InputDate = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(x));
//创建一个日历对象cDate
Calendar cDate = Calendar.getInstance();
//调用日历对象的方法得到本周第一天的值
cDate.setFirstDayOfWeek(Calendar.MONDAY);
//将时间传给日历对象
cDate.setTime(InputDate);
//创建一个日历对象firstDate
Calendar firstDate = Calendar.getInstance();
firstDate.setFirstDayOfWeek(Calendar.MONDAY);
firstDate.setTime(InputDate);
Calendar lastDate = Calendar.getInstance();
lastDate.setFirstDayOfWeek(Calendar.MONDAY);
lastDate.setTime(InputDate);
if(cDate.get(Calendar.WEEK_OF_YEAR)==1&&cDate.get(Calendar.MONTH)==11){
System.out.println("cDate="+cDate);
firstDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR)+1);
lastDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR)+1);
}
System.out.println("x="+x);
int week = cDate.get(Calendar.DAY_OF_WEEK);
System.out.println(week);
// int typeNum = cDate.get(Calendar.WEEK_OF_YEAR)-1; //返回第40周
int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);//返回41周
System.out.println("所在第几周"+typeNum);
firstDate.set(Calendar.WEEK_OF_YEAR, typeNum);
firstDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//所在周开始日期
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("sdf="+sdf);
// firstDate.getTime().add(Calendar.DAY_OF_WEEK, -1);
sdf.format(firstDate.getTime());
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(firstDate.getTime()));
lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
//所在周结束日期
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime()));
}
}