查找使用日期编号Java的月份中的哪一天 [英] Find what day in the month it is using day number Java

查看:109
本文介绍了查找使用日期编号Java的月份中的哪一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用一年中的某一天找到当月的日期?

How can i find the day of the month using the day of the year?

例如,第16天是1月16日。第35天是2月4日

For example day 16 is January 16. Day 35 is February 4

我的程序提示一年中的一天(1-365)和一年。

My program prompts for a day in the year (1-365) and year.

因此输入可以是第252天和2017年

So input can be day 252 and year 2017

我已经为闰年初始化了一个布尔值。

I already initialized a boolean for leap Years.

我已将此分为12个月( 1月至12月EX:如果输入的日期为1月31日,则使用if语句。

I already divided this into 12 months (Jan-Dec. EX: if the day input is 1-31 its January) using an if statement.

if(day >=1 && day <=31)
{
   month = 1;
   monthName = "January";
}

我的程序差不多完成了,这是我的最后一步,但我不知道如何实现这一目标。

My program is almost done, this is my last step but I have no idea how to go about doing this.

如何在不使用API​​或进口的情况下,一年中使用一天的一天找到一天?

How can I find the day in a month using day in a year WITHOUT using an API or imports?.

推荐答案

在一个非常基本的方法中,创建一个包含每个月天数的int数组。

In a very basic approach, create an int array containing the number of days in each month.

final int[] daysInMonth = new int[] { 31, 28, 31, 30, ... };

然后检查是否是闰年,执行:

Then check if it is a leap year, do:

daysInMonth[1]++;  // add an extra day in February

创建一个最初将包含你的int变量(称为remainingDays)输入。
你的代码应循环遍历创建的数组,首先检查 remainingDays - daysInMonth [i] (当月的天数)是否大于零。如果是,则从剩余的天数中减去该月的天数,然后转到下个月。如果不是,那么你的答案就在 remainingDays 变量中。

Create an int variable (called remainingDays) that initially will contain your input. And your code should loop through the created array, first checking if the remainingDays - daysInMonth[i] (the numbers of days in the month) is greater than zero. If it is, then subtract the number of days of that month from the remainder of days and go to the next month. if it is not, then your answer is in the remainingDays variable.

(因为你问的演练不是答案的代码)

(as you asked a walkthrough not the code that answers that)

这篇关于查找使用日期编号Java的月份中的哪一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆