「java判断跨年」java判断跨年周四在那年算那年
今天给各位分享java判断跨年的知识,其中也会对java判断跨年周四在那年算那年进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java 判断两个date 相隔多少年
public class Slob
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println(format(getDateLength("20070531", "20070601")));
System.out.println(format(getDateLength("20070531", "20070630")));
System.out.println(format(getDateLength("20070531", "20080531")));
System.out.println(format(getDateLength("20070531", "20080601")));
}
static int[] getDateLength(String from, String to)
{
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
java.util.Calendar c1 = java.util.Calendar.getInstance();
java.util.Calendar c2 = java.util.Calendar.getInstance();
try
{
c1.setTime(df.parse(from));
c2.setTime(df.parse(to));
} catch (java.text.ParseException e)
{
// XXX Auto-generated catch block
System.err.println("日期格式不正确,应该是yyyyMMdd");
e.printStackTrace();
}
int[] result = new int[3];
result[0] = (c2.get(java.util.Calendar.YEAR) - c1.get(java.util.Calendar.YEAR)) 0 ? 0 : c2
.get(java.util.Calendar.YEAR)
- c1.get(java.util.Calendar.YEAR);
result[1] = (c2.get(java.util.Calendar.MONTH) - c1.get(java.util.Calendar.MONTH)) 0 ? 0 : c2
.get(java.util.Calendar.MONTH)
- c1.get(java.util.Calendar.MONTH);
result[2] = (c2.get(java.util.Calendar.DAY_OF_MONTH) - c1.get(java.util.Calendar.DAY_OF_MONTH)) 0 ? 0 : c2
.get(java.util.Calendar.DAY_OF_MONTH)
- c1.get(java.util.Calendar.DAY_OF_MONTH);
return result;
}
static String format(int[] result)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
for (int i = 0; i result.length - 1; i++)
sb.append(result[i] + ",");
sb.append(result[result.length - 1] + "]");
return sb.toString();
}
}
java 如何判断 一年的最后一周是否跨年
给定一个时间范围判断周的范围并且跨年为新的一周即跨年时分为两周
public class Week{
public static void main(String[] args) {
SimpleDateFormat myFormatterExt = new SimpleDateFormat("yyyy-MM-dd");
Date b_date = null;
Date e_date = null;
try {
b_date = myFormatterExt.parse("2010-12-26");//开始时间
e_date = myFormatterExt.parse("2011-01-06");//结束时间
//e_date = myFormatterExt.parse("2011-01-08");
// date= myFormatterExt.parse("2010-12-31 00:00:00");
// String week = HNFDCommon.getWeek(date);
//System.out.println(week+"week");
} catch (ParseException e) {
e.printStackTrace();
}
b_date = getFirstDayOfWeek(b_date);
e_date = getLastDayOfWeek(e_date);
int dayOfweek = 0; //日期为星期几判断12月最后一个星期用
while (b_date.getTime() == e_date.getTime() || b_date.before(e_date))
{
String week = HNFDCommon.getWeek(b_date);
System.out.println(week+"ddd"+myFormatterExt.format(b_date));
String days = myFormatterExt.format(b_date).substring(8,10);
if(dayOfweek0){
b_date.setDate(b_date.getDate() +(7-dayOfweek));
dayOfweek = 0;
}else{
if(Integer.parseInt(week.substring(5,7))=50 (31- Integer.parseInt(days))7){
Date c_date = new Date();
c_date.setYear(b_date.getYear());
c_date.setMonth(b_date.getMonth());
c_date.setDate(31);
dayOfweek = getDayOfWeek(c_date);
b_date.setDate(b_date.getDate() +dayOfweek-1);
if(week.equals(HNFDCommon.getWeek(b_date))){
b_date.setDate(b_date.getDate() +(7-dayOfweek));
dayOfweek = 0;
}
}
else{
b_date.setDate(b_date.getDate() +7);
}
}
}
}
public static Date getLastDayOfWeek(Date date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy");
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()+6); // Sunday
if(!sd.format(date).equals(sd.format(c.getTime()))){
c.setTime(date);
}
return c.getTime();
}
public static Date getFirstDayOfWeek(Date date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy");
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Sunday
if(!sd.format(date).equals(sd.format(c.getTime()))){
c.setTime(date);
}
return c.getTime();
}
public static int getDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
//c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.get(Calendar.DAY_OF_WEEK);
}
}
HNFDCommon 类中:
public static String getWeek(Date date)
{
String rtn="";
int year;
int week;
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.setTime(date);
year=cd.get(Calendar.YEAR);
week = cd.get(Calendar.WEEK_OF_YEAR);
//System.out.println("ttt"+sd.format(cd.getTime())+"bbb"+week);
int vmonth = cd.get(Calendar.MONTH)+1;
if(week == 1 vmonth == 12){
cd.add(Calendar.WEEK_OF_YEAR,-1);
week =cd.get(Calendar.WEEK_OF_YEAR)+1;
}
if(week 6 vmonth == 1){
year--;
}
if(week 10){
rtn=year+"00"+week;
}
else{
rtn=year+"0"+week;
}
return rtn;
}
java怎样判断两个日期之间相差几周(支持跨年)?
/**
* 获取两个日期相差的天数
* @param big
* @param small
* @return
*/
public static int getTwoDatesDifOfDay(Date big,Date small){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(big);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(small);
if(cal1.get(Calendar.MONTH) != 11 cal2.get(Calendar.MONTH)==11){//跨年
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.DAY_OF_MONTH, 31);
return cal3.get(Calendar.DAY_OF_YEAR)-cal2.get(Calendar.DAY_OF_YEAR)+cal1.get(Calendar.DAY_OF_YEAR);
}else{
return cal1.get(Calendar.DAY_OF_YEAR)-cal2.get(Calendar.DAY_OF_YEAR);
}
}
/**
* 获取两个日期相差的周数
* @param big
* @param small
* @return
*/
public static int getTwoDatesDifOfWeek(Date big,Date small){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(big);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(small);
if(cal1.get(Calendar.MONTH) != 11 cal2.get(Calendar.MONTH)==11){//跨年
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.DAY_OF_MONTH, 31);
return cal3.get(Calendar.WEEK_OF_YEAR)-cal2.get(Calendar.WEEK_OF_YEAR)+cal1.get(Calendar.WEEK_OF_YEAR);
}else{
return cal1.get(Calendar.WEEK_OF_YEAR)-cal2.get(Calendar.WEEK_OF_YEAR);
}
}
java判断跨年的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java判断跨年周四在那年算那年、java判断跨年的信息别忘了在本站进行查找喔。