将一个天数转换成日期的方法

huangapple go评论112阅读模式
英文:

a method that converts a daynumber into a date

问题

Here's the translated code portion:

public class DateOperations {
	
	private final static String[] MONTHS = {"January", "February", "March", "April", "May", "June", "July", "August", 
			"September", "October", "November", "December"};
	
	private static final int[] NUMBERDAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	public static String calculationDateWithDayNumber(int day) {
		int month = 0;
		for (int i = 0; day >= NUMBERDAYS[i]; i++) {
			day -= NUMBERDAYS[i];
			++month;
		}
			 
		String nameMonth = MONTHS[month];
			
		String datum = String.format("%d %s", day, nameMonth);
		
		return datum;
	}
}

Please note that I've removed the HTML escape codes (e.g., ") and corrected the code formatting.

英文:

A number between 1 and 365 is requested from the user. The number represents the day number of the year. The corresponding date is displayed.

I get stuck on the method calculationDateWithDayNumber

An example: the number 105 should be converted to date 15 April. My result is -31 February.
Where does it go wrong?

public class DateOperations {
	
	private final static String[] MONTHS = {"January", "February", "March", "April", "May", "June", "July", "August", 
			"September", "October", "November", "December"	};
	
	private static final int[] NUMBERDAYS = {31,28,31,30,31,30,31,31,30,31,30,31};
	
	public static String calculationDateWithDayNumber(int day)
	{
		int month = 0;
		for (int i=0; day>=NUMBERDAYS[i] ;i++)	
			{day=-NUMBERDAYS[i];
			++ month;}
			 
			String nameMonth = MONTHS[month];

			 
			
		String datum = String.format("%d%s",day, nameMonth);
		
		return datum;
	}

}

答案1

得分: 2

你可以使用java.time API的ofYearDay方法轻松实现这个功能。由于你提到数字范围是1到365,你可能是假设不是闰年,所以可以将任何非闰年作为第一个参数传递:

public static String getMonthDayFromDayOfYear(int dayOfYear) {
    // 2019年是非闰年
    LocalDate date = LocalDate.ofYearDay(2019, dayOfYear);

    // "ddMMMM" 似乎是你期望的格式
    return DateTimeFormatter.ofPattern("ddMMMM").withLocale(Locale.US).format(date);
}
英文:

You can do this very easily with the java.time API, using ofYearDay. Since you said that the number can be from 1 to 365, you are probably assuming a non-leap year, so you can pass any non-leap year as the first parameter:

public static String getMonthDayFromDayOfYear(int dayOfYear) {
    // 2019 is a non-leap year
    LocalDate date = LocalDate.ofYearDay(2019, dayOfYear);

    // "ddMMMM" seems to be your desired format
    return DateTimeFormatter.ofPattern("ddMMMM").withLocale(Locale.US).format(date);
}

答案2

得分: 2

I guess you are practicing.
Two fixes are needed, see below comments in code

day=-NUMBERDAYS[i] means you are assign value of (-1*NUMBERDAYS[i]) in day. But I think you should minus NUMBERDAYS[i] from day like day - NUMBERDAYS[i] and assign the value in day like day = day - NUMBERDAYS[i] or using shorthand operator like day -= NUMBERDAYS[i]

public static String calculationDateWithDayNumber(int day) {
int month = 0;
for (int i = 0; day > NUMBERDAYS[i]; i++) { // Fix here, remove equals since day can be last day of month
day = day - NUMBERDAYS[i]; // Fix here, minus the NUMBERDAYS[i] from day
++month;
}
String nameMonth = MONTHS[month];
String datum = String.format("%d %s", day, nameMonth);
return datum;
}

Output: 15 April

英文:

I guess you are practicing.
Two fixes are needed, see below comments in code

day=-NUMBERDAYS[i] means you are assign value of (-1*NUMBERDAYS[i]) in day. But I think you should minus NUMBERDAYS[i] from day like day - NUMBERDAYS[i] and assign the value in day like day = day - NUMBERDAYS[i] or using shorthand operator like day -= NUMBERDAYS[i]

  public static String calculationDateWithDayNumber(int day) {
    int month = 0;
    for (int i = 0; day > NUMBERDAYS[i]; i++) {  // Fix here, remove equals since day can be last day of month
      day = day - NUMBERDAYS[i]; // Fix here, minus the NUMBERDAYS[i] from day
      ++month;
    }
    String nameMonth = MONTHS[month];
    String datum = String.format("%d %s", day, nameMonth);
    return datum;
  }

Output: 15 April

答案3

得分: 1

java.time以来,此任务可以在不进行任何自定义计算的情况下完成(这在任何情况下都不是微不足道的)。

基本上,您只需获取今天的日期并调整年份保留年份的一年中的某一天。请参考以下示例:

public static String getDateFromDayOfYear(int dayOfYear) {
	// 获取“今天”的日期(基本上只是为了获取当前年份)
	LocalDate localDate = LocalDate.now()
									// 并使用参数调整一年中的某一天
									.withDayOfYear(dayOfYear);
	// 然后以所需格式(在此处为GER/DE)返回该日期的字符串表示形式
	return localDate.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));
}

main方法中使用它,如下所示:

public static void main(String[] args) {
	int apr14 = 105;
	System.out.println(getDateFromDayOfYear(apr14));
}

将输出:

14.04.2020

显然,这不是您所期望的输出,因为您的任务未考虑闰年(即那些由于2月29日而具有366天的年份,例如2020年)。

英文:

Since java.time this task can be done without any custom calculation (which wouldn't be trivial at all).

Basically, you can just take the date of today and adjust the day of year preserving the year. Have a look at this example:

public static String getDateFromDayOfYear(int dayOfYear) {
	// take "today" (basically just to have the current year
	LocalDate localDate = LocalDate.now()
									// and adjust the day of year using the argument
									.withDayOfYear(dayOfYear);
	// then return a String representation of that date in a desired format (GER/DE here)
	return localDate.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));
}

Using it in a main method like

public static void main(String[] args) {
	int apr14 = 105;
	System.out.println(getDateFromDayOfYear(apr14));
}

would output

14.04.2020

which is obviously not your desired output because your task doesn't consider leap years
(those with 366 days due to a Februray 29th, like in 2020).

答案4

得分: -1

你需要设置年份以处理闰年。

英文:

I think you need the below code snippet :

public static void main(String[] args) {
  int dayOfYear = 60;
  int year = 2019;
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
  System.out.println("Day " + dayOfYear + " of the current year = " + calendar.getTime());

}

You need to set the year, to take of the leap year

huangapple
  • 本文由 发表于 2020年8月11日 13:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定