输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
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
常规思路,判闰年,逐月减,小零止,规整出
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();
}
} 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()));
}
}
} 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();
}
}
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;
}
}
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);
}
}
}
}
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()));
}
}
}
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)); } }
/**
只要建立一个数组,用于查表就好,把闰年的情况独立出来,也就是说把二月改一下即可。
而且天数只会最多有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);
}
}
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;
}
}