首页 > 试题广场 >

打印日期

[编程题]打印日期
  • 热度指数:46972 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给出年分m和一年中的第n天,算出第n天是几月几号。

输入描述:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。


输出描述:
可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
示例1

输入

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

输出

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner in = new Scanner(System.in);
  5.         int[][] a = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  6.         {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
  7.         //当年数不能被100整除时,若其能被4整除或400整除时,则为闰年
  8.         while (in.hasNextInt()) { // 注意 while 处理多个 case
  9.             int year = in.nextInt();
  10.             int _day = in.nextInt();

  11.             PrintDate(a,_day,year);
  12.            
  13.         }
  14.     }
  15.     //判断输入的年份是否是闰年
  16.     public static boolean IsRunYear(int year) {
  17.         if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  18.             return true;
  19.         }
  20.         return false;
  21.     }
  22.     //根据年份,第几天等信息打印日期
  23.     public static void PrintDate(int[][] a, int _day, int year) {
  24.         int j = IsRunYear(year) ? 1 : 0;
  25.         int month = 0;
  26.         for (int i = 0 ; i < a[0].length ; ++i) {
  27.             if (_day > a[j][i]) {
  28.                 _day = _day - a[j][i];
  29.             } else {
  30.                 month = i;
  31.                 break;
  32.             }
  33.         }
  34.         System.out.println(year + "-" + (month < 10 ? "0" + month : month) + "-" +(_day < 10 ? "0" + _day : _day));
  35.     }
  36. }
发表于 2023-02-25 22:13:41 回复(0)
import java.util.*;

public class Main{
    
    public static boolean isrunnian(int year){
        if((year%100!=0&&year%4==0)||year%400==0){
            return true;
        }else{
            return false;
        }
    }
    
    
    public static void main(String[] args){
        
        //闰年,非闰年
        int[][] date={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
        
        Scanner scanner=new Scanner(System.in);
        int year=scanner.nextInt();
        int days=scanner.nextInt();
        if(days<=31){
            System.out.print(year+"-01-"+days);
        }else{
            if(isrunnian(year)){
                int month=1;
                while(days>date[0][month-1]){
                    days=days-date[0][month-1];
                    month++;
                }
                if(days<10&&month>=10){
                    System.out.print(year+"-"+month+"-0"+days);
                }
                if(days<10&&month<10){
                    System.out.print(year+"-0"+month+"-0"+days);
                }
                if(days>=10&&month<10){
                    System.out.print(year+"-0"+month+"-"+days);
                }
                if(days>=10&&month>=10){
                   System.out.print(year+"-"+month+"-"+days);
                }
                
            }else{
                int month=1;
                while(days>date[1][month-1]){
                    days=days-date[1][month-1];
                    month++;
                }
                 if(days<10&&month>=10){
                    System.out.print(year+"-"+month+"-0"+days);
                }
                if(days<10&&month<10){
                    System.out.print(year+"-0"+month+"-0"+days);
                }
                if(days>=10&&month<10){
                    System.out.print(year+"-0"+month+"-"+days);
                }
                if(days>=10&&month>=10){
                   System.out.print(year+"-"+month+"-"+days);
                }
            }
        }
        
        
        
        
    }
}
发表于 2022-02-24 18:08:33 回复(0)
常规思路,判闰年,逐月减,小零止,规整出
public class Main {

    public static int isYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return 1;
        }else {
            return 0;
        }
    }

    public static void main(String[] args) {
        int monthDay[][] = {{31,28,31,30,31,30,31,31,30,31,30,31},
                {31,29,31,30,31,30,31,31,30,31,30,31}};
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int year,dayNumber;
            year = scanner.nextInt();
            dayNumber = scanner.nextInt();

            for (int month = 0;month<12;month++){
                if ((dayNumber - monthDay[isYear(year)][month])>0) {
                    dayNumber -= monthDay[isYear(year)][month];
                }else {
//                    System.out.println(year+"-"+(month+1)+"-"+dayNumber);
                    System.out.printf("%04d-%02d-%02d\n",year,month+1,dayNumber);
                    break;
                }
            }
        }
        scanner.close();
    }
} 

发表于 2021-03-09 15:15:16 回复(0)
Java8新的时间API确实好用
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int year = scanner.nextInt();
            int days = scanner.nextInt();
            LocalDate day = LocalDate.ofYearDay(year, days);
            DecimalFormat f = new DecimalFormat("00");
            System.out.println(day.getYear()+"-"+f.format(day.getMonth().getValue())+"-"+f.format(day.getDayOfMonth()));
        }
    }
}


发表于 2020-03-18 10:53:50 回复(0)
import java.io.*;

public class Main{
    private static boolean isLeap(int n) {
        if (n%100!=0&&n%4==0) {
            return true;
        } else if(n%400==0){
            return true;
        }else
        return false;
    }
    private static int[] monthAndDay(int day,int[] month) {
        int[] date = new int[2];
        for (int i = 0; i < 12; i++) {
            int j = month[i];
            
            if ((day-=j)<=0) {
                date[0]=i+1;
                date[1]=day+j;
                break;
            }
        }
        return date;
    }
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] line = reader.readLine().split(" ");
        int[] leap    = {31,29,31,30,31,30,31,31,30,31,30,31};
        int[] notLeap = {31,28,31,30,31,30,31,31,30,31,30,31};
        int year = Integer.parseInt(line[0]);
        int day = Integer.parseInt(line[1]);
        int[] date;
        
        if (isLeap(year)) {
            date = monthAndDay(day, leap);
        } else {
            date = monthAndDay(day, notLeap);

        }
        System.out.printf("%04d-%02d-%02d", year,date[0],date[1]);
        
        
    }
}
 
发表于 2019-01-18 20:47:15 回复(0)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    String line = null;
    int day[]={31,29,31,30,31,30,31,31,30,31,30,31};
    while ((line = input.readLine()) != null) {
        int year= Integer.parseInt(line.split(" ")[0]);
        int date= Integer.parseInt(line.split(" ")[1]);
        if((year%100!=0&&year%4==0)||year%400==0){
            day[1]=29;
        }else{
            day[1]=28;
        }
        String d=year+"-";
        for(int i=0;i<12;i++){
            if(date<=day[i]){
                if(i<9){
                    d+="0"+(i+1)+"-";
                }else{
                    d+=(i+1)+"-";
                }
                if(date<10){
                    d+="0"+date;
                }else{
                    d+=date;
                }
                System.out.println(d);
                break;
            }else{
                date-=day[i];
            }
        }
    }
    input.close();
}

}

发表于 2018-04-20 17:45:43 回复(0)
Calendar类中有一个方法:getActualMinimum(int field),可以获取每一个月有多少天
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
  
        while ((line = br.readLine()) != null) {
              
            String[] arrStr = line.split(" ");
              
            int year = Integer.parseInt(arrStr[0]);
            int n = Integer.parseInt(arrStr[1]);
            String s = test(year, n); 
            System.out.println(s);
              
        }
	
	}
	public static String test(int year, int n) {
		
		String str = "-1";
		int month = 1;
		int maxMonthDay = 0;
		int amountDay = 0;
		
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = new GregorianCalendar();
		
		try {
			/*
			 * 获取月份
			 */
			while(true){
				//year-month-1
				str = year + "-" + month + "-1";
				//字符串转为日期
				Date date = dateformat.parse(str);
				calendar.setTime(date);
				
				//获取每一个月的天数
				maxMonthDay = calendar.getActualMaximum(Calendar.DATE);
				if(n<(amountDay+maxMonthDay)){
					break;
				}
				amountDay += maxMonthDay;
				month++;
			}
			
			int day2 = n - amountDay;
			str = year + "-" + month + "-" + day2;
			Date d = dateformat.parse(str);
            String s = dateformat.format(d);
			return s;
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}


发表于 2017-08-16 16:19:09 回复(0)
import java.util.Scanner;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int year = scanner.nextInt();
		int day = scanner.nextInt();
		int nowMonth = 0;
		int nowDay = 0;
		/*
		 * 建立一个数组,表示出所有的月份的天数
		 */
		int[] allMonthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
			if (day > 59) {
				allMonthDays[1] = 29;
			}
		}
		int sum = 0;
		int i;
		for (i = 0; i < allMonthDays.length; i++) {
			if (sum < day) {
				sum += allMonthDays[i];
			} else {
				nowMonth = i;
				nowDay = allMonthDays[i - 1] - sum + day;
				break;
			}
		}
		if (i == 12) {
			nowMonth = 12;
			nowDay = allMonthDays[i - 1] - sum + day;
		}

		if (nowDay < 10 && nowMonth >= 10) {
			System.out.println(year + "-" + nowMonth + "-0" + nowDay);
		} else if (nowDay < 10 && nowMonth < 10) {
			System.out.println(year + "-0" + nowMonth + "-0" + nowDay);
		} else if (nowDay >= 10 && nowMonth < 10) {
			System.out.println(year + "-0" + nowMonth + "-" + nowDay);
		} else {
			System.out.println(year + "-" + nowMonth + "-" + nowDay);
		}
	}
}


发表于 2017-06-18 18:24:16 回复(0)
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
        int year=sc.nextInt();
        int day=sc.nextInt();
        while(year<1 || year>3000 ||day<1 || day>366)
        {
            year=sc.nextInt();
            day=sc.nextInt();
        }
        int [] month={31,28,31,30,31,30,31,31,30,31,30,31};
        if((year%4==0 && year%100!=0 ) || year%400==0)
        {
        month[1]=29;
        }
        int sumD=0;
        int dd=0;
        int m=0;
        for(int i=0;i<month.length;i++)
        {
        sumD+=month[i];
        if(day>sumD)
        {
        ;
        }
        else
        {
        dd=day+month[i]-sumD;
        m=i+1;
        break;
        }
        }
        if(m<10)
        {
        if(dd<10)
        {
        System.out.println(year+"-"+"0"+m+"-"+"0"+dd);
        }
        else
        {
        System.out.println(year+"-"+"0"+m+"-"+dd);
        }
         
        }
        else
        {
        if(dd<10)
        {
        System.out.println(year+"-"+m+"-"+"0"+dd);
        }
        else
        {
        System.out.println(year+"-"+m+"-"+dd);
        }
        }
       
    }
        
    }
}

发表于 2017-05-30 16:21:22 回复(0)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cld = Calendar.getInstance();
		while (in.hasNext()) {
			cld.set(in.nextInt(), 0, in.nextInt());
			System.out.println(sdf.format(cld.getTime()));
		}
	}
} 
发表于 2017-05-20 01:37:38 回复(0)
set(year, 0, 0, 0,0, 0);          //set(int year, int month, int date, int hourOfDay, int minute) 
set(Calendar.DAY_OF_YEAR, day);   //传入年份和这一天
格式化输出即可
发表于 2017-05-11 12:26:48 回复(0)
就Java而言,是可以不用转换写那么多的……直接生成是可以的……
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;


public class Main {
	
	private static String defaultDatePattern = "yyyy-MM-dd"; 
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main m = new Main();
		while(sc.hasNext()){
			int year = sc.nextInt();
			int day = sc.nextInt();
			m.outCalendar(year, day);
		}
		sc.close();
	}

	public void outCalendar(int year, int day) {
		Calendar c = Calendar.getInstance();
		c.set(year, 0, day);
		Date d = c.getTime();
		SimpleDateFormat df=new SimpleDateFormat(defaultDatePattern);
		System.out.println(df.format(d));
	}
	
} 


发表于 2017-04-09 22:19:46 回复(0)
/**
只要建立一个数组,用于查表就好,把闰年的情况独立出来,也就是说把二月改一下即可。
而且天数只会最多有366天,也就是加一年而已,完全不用再考虑第二年情况!!
*/
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			fun(in.nextInt(), in.nextInt());
		}
		in.close();
	}
	
	public static void fun(int a, int b){
		int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		if(a % 4 == 0 && a % 100 != 0 || a % 400 == 0)
			month[1] = 29;
		int i;
		for(i = 0; i < 12; i++){
			if(b <= month[i])
				break;
			else
				b -= month[i];
		}
		if(i == 11 && b > 31)
			a++;
		String str = String.format("%d-%02d-%02d",a, i+1, b);
		System.out.println(str);
	}
}


编辑于 2017-02-06 17:48:11 回复(0)
import java.util.Scanner;

public class Main
{
    static int[] M = {31,28,31,30,31,30,31,31,30,31,30,31};
    static int[] N = {31,29,31,30,31,30,31,31,30,31,30,31};
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String[] line = scan.nextLine().split(" ");
            if(line.length == 2){
                int year = Integer.parseInt(line[0]);
                int index = Integer.parseInt(line[1]);
                getDate(year,index);
            }
        }
    }
    
    private static void getDate(int _year, int _index){
        if(_year < 1 || _year > 3000)
            return;
        if(_index < 1 || _index > 366)
            return;

		System.out.print(_year+"-");
        if(!isRunNian(_year)){
            getMD(_index,M);
        }else{
            getMD(_index,N);
        }
    }

	private static void getMD(int _index,int[] A){
		int day = _index;
        for(int i=0;i<A.length;i++){
            if(day > A[i]){
                day -= A[i];
            }else{
                System.out.println(trans((i+1))+"-"+trans(day));
                break;
            }
        }
	
	}

	private static String trans(int _x){
		return _x < 10?"0"+_x:String.valueOf(_x);
	}
    
    
    private static boolean isRunNian(int _year){
        if(_year % 400 == 0)
        	return true;
        
        if(_year % 4 == 0){
        	if(_year % 100 == 0)
                return false;
            return true;
        }
        return false;
    }
    
    
    
}

发表于 2016-12-09 15:41:40 回复(0)