当前位置:编程学习 > JAVA >>

日期算法

页面上有一个这一年的多少周,代码中怎么算出是这一年的几月几号 到 几月几号 ? 算法 --------------------编程问答-------------------- SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, 23);        
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println(sdf.format(cal.getTime())); --------------------编程问答--------------------
引用 1 楼 soton_dolphin 的回复:
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, 23);        
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println(sdf.format(cal.getTime()));


+1024 --------------------编程问答-------------------- 如果是js的话,返回某一年的第几周的第一天的方法^_^:

function getDateByYearAndWeek(year, weeks){
    var date = new Date(year, 0, 1);
    if (weeks == 1)
        return date;
    var weekday = date.getDay();
    var time = date.getTime();
    time += ((weeks - 1) * 7 - weekday) * 24 * 3600000;
    date.setTime(time);
    return date;
}
--------------------编程问答--------------------

        SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.WEEK_OF_YEAR, 23);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        System.out.println(sdf.format(cal.getTime()));//周日
        cal.set(Calendar.DAY_OF_WEEK, 7);
        System.out.println(sdf.format(cal.getTime()));//周一
--------------------编程问答--------------------
引用 4 楼 ghostkngiht 的回复:

        SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.WEEK_OF_YEAR, 23);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        System.out.println(sdf.format(cal.getTime()));//周日
        cal.set(Calendar.DAY_OF_WEEK, 7);
        System.out.println(sdf.format(cal.getTime()));//周一

cal.set(Calendar.WEEK_OF_YEAR, 23);//这个可以设置一个变量,就是你想要的结果,当你输入第几周的时候就会输出周日到周一的日期 --------------------编程问答--------------------
Scanner sc1=new Scanner(System.in);
System.out.println("请输入年份:");
int year=sc1.nextInt();
Scanner sc=new Scanner(System.in);
System.out.println("请输入第几周:");
int week=sc.nextInt();
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy"); 
Calendar cal = Calendar.getInstance();
cal.set(year, 1, 1);
cal.set(Calendar.WEEK_OF_YEAR, week);        
cal.set(Calendar.DAY_OF_WEEK, 1);        
System.out.println(sdf.format(cal.getTime()));//周日       
cal.set(Calendar.DAY_OF_WEEK, 7);        
System.out.println(sdf.format(cal.getTime()));//周一

先输入哪一年,然后再输入第几周,就会出现那一年的日期了。
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,