根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:,空间复杂度:
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.time.LocalDate; import java.util.Calendar; import java.util.Date; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); String[] split = line.split(" "); int year = Integer.parseInt(split[0]); int month = Integer.parseInt(split[1]); int day = Integer.parseInt(split[2]); System.out.println(getDays1(year, month, day)); /*System.out.println(getDays2(year, month, day)); System.out.println(getDays3(year, month, day));*/ } /** * 通过日历类计算 * * @param year * @param month * @param day * @return */ private static int getDays1(int year, int month, int day) { // 创建日历类对象 Calendar calendar = Calendar.getInstance(); // 设置日历,month从0开始,所以要-1 calendar.set(year, month - 1, day); // 计算当前日期为该年的多少天 return calendar.get(Calendar.DAY_OF_YEAR); } /** * 通过日期类计算 * * @param year * @param month * @param day * @return */ private static long getDays2(int year, int month, int day) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day); // 转换为日期类 Date time1 = calendar.getTime(); calendar.set(year, 0, 1); Date time2 = calendar.getTime(); // 日期对象转换为毫秒进行计算 long l = time1.getTime() - time2.getTime(); // 换算成天数 return (1 + l / (1000 * 3600 * 24)); } /** * 通过LocalDate计算 * * @param year * @param month * @param day * @return */ private static int getDays3(int year, int month, int day) { LocalDate localDate = LocalDate.of(year, month, day); int dayOfYear = localDate.getDayOfYear(); return dayOfYear; } }
import java.util.Scanner; import java.time.LocalDate; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int year = in.nextInt(); int month = in.nextInt(); int days = in.nextInt(); LocalDate localDate = LocalDate.of(year,month,days); int dayOfYear = localDate.getDayOfYear(); System.out.println(dayOfYear); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int year = in.nextInt(), month = in.nextInt(), day = in.nextInt(), sum = 0; int[] months = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 11}; if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) months[1] = 29; for(int i = 1; i < month; i++){ sum += months[i-1]; } sum += day; System.out.println(sum); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static int days[] = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int year = in.nextInt(); int month = in.nextInt(); int date = in.nextInt(); int sum = 0; for (int i = 0; i < month; i++) { sum += days[i]; } sum += date; if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { if (month > 3) { sum++; } } System.out.println(sum); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while(in.hasNext()){ int nian = in.nextInt(); int yue = in.nextInt(); int ri = in.nextInt(); HashMap<Integer,Integer> map = new HashMap<>(); map.put(1,31); // map.put(2,31); map.put(3,31); map.put(4,30); map.put(5,31); map.put(6,30); map.put(7,31); map.put(8,31); map.put(9,30); map.put(10,31); map.put(11,30); map.put(12,31); //1、能被4整除并且不能被100整除 //2、能被400整除 if(((nian%4)==0&&(nian%100)!=0)||(nian%400==0)){ map.put(2,29); }else{ map.put(2,28); } int count = 0; for(int i=1;i<=yue;i++){ if(i==yue){ count+=ri; }else{ count+=map.get(i); } } System.out.println(count); } } }
import java.io.*; import java.io.IOException; import java.util.*; public class Main { private final static int[] dayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private final static int[] preSum = new int[dayOfMonth.length + 1]; public static void main(String[] args) throws IOException { for (int i = 1; i < preSum.length; ++i) { preSum[i] = preSum[i - 1] + dayOfMonth[i - 1]; } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while(Objects.nonNull(str = br.readLine())) { String[] array = str.split(" "); int year = Integer.parseInt(array[0]), month = Integer.parseInt(array[1]), day = Integer.parseInt(array[2]); boolean isRun = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); if (isRun && month > 2) day += preSum[month - 1] + 1; else day += preSum[month - 1]; System.out.println(day); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().split(" "); int year = Integer.parseInt(params[0]); int month = Integer.parseInt(params[1]); int day = Integer.parseInt(params[2]); int sum = 0; boolean flag = isRunNian(year); for(int i = 1; i <= month; i++){ sum += monthToDay(i - 1 , flag); } sum += day; System.out.println(sum); } public static boolean isRunNian(int year){ if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)){ return true; } return false; } public static int monthToDay(int month, boolean flag){ switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if(flag){ return 29; }else{ return 28; } } return 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 br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().split(" "); String year = input[0]; String mon = input[1]; String day = input[2]; int days = 0; for (int i = Integer.parseInt(mon) - 1 ; i >= 1; i--) { if (i % 2 == 0 && i != 2) { days += 30; } else if (i == 2) { if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0 || Integer.parseInt(year) % 400 == 0) { days += 29; } else { days += 28; } } else { days += 31; } } days += Integer.parseInt(day); System.out.println(days+""); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int month = sc.nextInt(); int day = sc.nextInt(); int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int[] months = new int[days.length]; int sum = 0; for (int i = 0; i < days.length; i++) { months[i] = sum; sum += days[i]; } int flag = 0; if ((year % 100 != 0 && year % 4 == 0 || year % 400 == 0) && month > 2) flag++; System.out.println(flag + months[month - 1] + day); } }
import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner scan = new Scanner(System.in); int year = scan.nextInt(); int month = scan.nextInt(); int day = scan.nextInt(); int sum =0; int[]array={31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month-1;i++){ sum+=array[i]; } sum+=day; if(isRunYear(year)&&month>2){ sum =sum+1; } System.out.println(sum); } public static boolean isRunYear(int year){ if(year%4==0&&year%100!=0){ return true; }else if(year%100==0&&year%400==0){ return true; } return false; } }
import java.util.Scanner; /** 题目:输入日期,计算是这一年的第几天 分析: 1.按空格,解析出年月日 2.判断今年是不是润年的方法:不能被100整除但是必须被4整除 或 能被400整除 3.写出闰年和非闰年的两个int数组,每一个元素代表每一个月的天数 4.遍历月数,每月对应的最大天数为:int[0到month-1-1] 累加起来 比如:12月,那么遍历数组中,1-11月,进行累加,对应数组索引为0-10 最后加上日就是答案了 **/ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); int[] runArr = {31,29,31,30,31,30,31,31,30,31,30,31}; int[] notRunArr = {31,28,31,30,31,30,31,31,30,31,30,31}; String[] date = input.split(" "); int year = Integer.parseInt(date[0]); int month = Integer.parseInt(date[1]); int day = Integer.parseInt(date[2]); boolean flag = isRun(year); int result = 0; //计算前n个月的天数 //12月,那么就要计算 1-11月的,那么对应的索引就是 0-10 if(flag){ for (int i = 0; i < month-1; i++) { result += runArr[i]; } }else{ for (int i = 0; i < month-1; i++) { result += notRunArr[i]; } } //加上日 result += day; System.out.println(result); } public static boolean isRun(int year){ if(year%400 == 0){ return true; }else if(year%100 !=0 && year%4==0){ return true; } return false; } }
import java.util.Scanner; public class Main{ public static int yearTransform(int year, int month){ switch(month){ case 1: return 31; case 2: { if(year%4 == 0 && year%100 != 0)return 29; else return 28; } case 3: return 31; case 4: return 30; case 5: return 31; case 6: return 30; case 7: return 31; case 8: return 31; case 9: return 30; case 10: return 31; case 11: return 30; case 12: return 31; default: return 0; } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int month = sc.nextInt(); int day = sc.nextInt(); for(int i = 1; i<month; i++){ day += yearTransform(year, i); } System.out.println(day); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int y = sc.nextInt(); int m = sc.nextInt(); int d = sc.nextInt(); int[] month = new int[]{31,28,31,30,31,30,31,31,30,31,30,31}; if((y % 4 == 0 && y % 100 != 0) ||(y % 400 ==0)){ month[1] = 29; } int day = 0; for(int i = 0;i < m - 1;i++){ day += month[i]; } day +=d; System.out.println(day); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { String s = sc.nextLine(); String[] s1 = s.split(" "); solution(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), Integer.parseInt(s1[2])); } } private static void solution(int year, int month, int day) { int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)) { days[1] = 29; } int daysToNow = 0; for (int i = 0; i < month - 1; i++) { daysToNow += days[i]; } daysToNow += day; System.out.println(daysToNow); } private static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } }