java做个跨年代码 程序员的跨年代码
我做了个输入日期,然后计算距离1月1号过了多少天的JAVA代码,求会JAVA的网友帮我看看哪里错了
第一,你没有考虑平年闰年的问题,闰年二月有29天
创新互联公司自成立以来,一直致力于为企业提供从网站策划、网站设计、网站制作、做网站、电子商务、网站推广、网站优化到为企业提供个性化软件开发等基于互联网的全面整合营销服务。公司拥有丰富的网站建设和互联网应用系统开发管理经验、成熟的应用系统解决方案、优秀的网站开发工程师团队及专业的网站设计师团队。
第二,你的break放在return之后,java编译器会报错,看你写代码这么绕,java基础应该不差啊,那么问你,return之后的语句有意义吗?
第三,打印流在整个类中的使用中如果关闭一次,下次就无法打开,所以最后再关闭打印流,在输入月份的时候不要关闭打印流,就你的input.close()去掉,完了就对了
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给定两个日期,求两个日期之间的日期段,以月为分隔~!
用java的日历类
public static void main(String[] args) throws ParseException {
String d1= "2011-05-12";
String d2="2011-08-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(d1);
Date date2 = format.parse(d2);
Calendar cal =Calendar.getInstance();
cal.setTime(date1);
int day = cal.get(Calendar.DAY_OF_MONTH);
while(cal.getTime().before(date2)){
String begin = format.format(cal.getTime());
int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, max);
String end = cal.getTime().before(date2) ? format.format(cal.getTime()) : d2;
System.out.println(begin+"~"+end);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, day);
}
}
做个日期查询,判断开始日期与终止日期范围必须在一个月之内,用java代码
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
public class DateTest {
/**
* 判断是否在同一个月
* @param startDate yyyy-MM-dd
* @param endDate yyyy-MM-dd
* @return false:不在同一个月内,true在同一个月内
*/
public static boolean isMonth(String startDate,String endDate){
if(margin(startDate, endDate)31){
return false;
}
int startMonth = Integer.parseInt(startDate.substring(5, 7));
int endMonth = Integer.parseInt(endDate.substring(5, 7));
if(startMonth==endMonth){
return true;
}else{
return false;
}
}
/**
* 计算开始日期和结束日期差
* @param startDate yyyy-MM-dd
* @param endDate yyyy-MM-dd
* @return
*/
private static int margin(String startDate,String endDate){
ParsePosition pos = new ParsePosition(0);
ParsePosition pos2 = new ParsePosition(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ds = sdf.parse(startDate, pos);
Date de = sdf.parse(endDate, pos2);
long l = de.getTime()-ds.getTime();
int margin = (int)(l/24*60*60*1000);
return margin;
}
/**
* main方法测试
* @param args
*/
public static void main(String[] args) {
System.out.println(DateTest.isMonth("2014-10-17", "2014-10-25"));
System.out.println(DateTest.isMonth("2014-10-17", "2014-12-25"));
}
}
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做个跨年代码 程序员的跨年代码
网页路径:http://azwzsj.com/article/hipipg.html