纬度打击 2020-11-16 16:22 采纳率: 88.9%
浏览 15
已采纳

不知道为什么运行错误

package work;


public class two {

	public static void main(String[] args) {
		
		String date = "";
    	int totalDay = 0;
    	long totalMilliseconds = System.currentTimeMillis();
    	totalDay = (int)((totalMilliseconds / 1000 / 60 / 60 + 8) / 24 );
    	
    	int year = 0;
    	int month = 0;
    	int day = 0;
    	for ( year = 1970; totalDay <= 366 ; year++) {
    		if (year % 4 == 0) 
    			totalDay = totalDay - 366;
    		else 
    			totalDay = totalDay - 365;
    	}
    	
    	int[] monthDay = new int[12];
    	monthDay[0] = 31;
    	monthDay[2] = 31;
    	monthDay[3] = 30;
    	monthDay[4] = 31;
    	monthDay[5] = 30;
    	monthDay[6] = 31;
    	monthDay[7] = 31;
    	monthDay[8] = 30;
    	monthDay[9] = 31;
    	monthDay[10] = 30;
    	monthDay[11] = 31;
    	
    	if (year % 4 != 0 ) 
    		monthDay[1] = 28;
    	else
    		monthDay[1] = 29;
    	
    	for (month = 1; totalDay >= 0; month++) {
    		if (totalDay <= monthDay[month-1]) {
    			day = totalDay;
    		}
    		totalDay -= monthDay[month-1];
    	}
    	
    	String yearz = Integer.toString(year);
    	String monthz = Integer.toString(month);
    	String dayz = Integer.toString(day);
    	date =yearz + " " + monthz + " " + dayz;
    	System.out.println(date);
    
	}

 

  • 写回答

14条回答 默认 最新

  • qq_25376897 2020-11-16 17:26
    关注

    如果你只是想格式化打印当前年月日,几行代码就可以了,真心不用这么麻烦的。

    public static void main(String[] args) {
        Date date = new Date();
        //格式化日期
        String strDateFormat = "yyyy MM dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大
  • ¥15 使用spss做psm(倾向性评分匹配)遇到问题
  • ¥20 vue+UEditor附件上传问题
  • ¥15 想做个WPS的自动化代码,不知道能做的起不。
  • ¥15 uniApp,生成安卓安卓包后,黑暗主题中间内容不生效,底部导航正常
  • ¥15 斯坦福自动漂移非线性模型反解
  • ¥15 学习Python如何找兼职
  • ¥15 python结合Matlab仿真忆阻器