日獲得一個月我有一個像34
輸入,這意味着它的3rd day of February
,而是如何通過採取day of year
並獲得month name
或一個月month value like 0 for January
編程確定在Java中有沒有在Java中處理這個任何API。我搜索了Calendar class
,但沒有找到任何。如何根據今年
如何根據今年
回答
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR, 34);
System.out.println(cal.get(Calendar.MONTH));
將返回1(因爲月份以0開始)。
此帳戶是否爲閏年? – Adarsh
如果您相應地設置年,則爲。GetInstance()會給你包括當年的當前日期。 –
試試這個
Calendar cal = Calendar.getInstance();
cal.set(2014, 0, 1);
cal.add(Calendar.DAY_OF_YEAR, 34);
重新發明輪子:-(
package com.test;
public class FindMonthByTotalNumberOfDays {
public static void main(String[] args) {
int input = 89;
int monthDigit = extractMonthDigit(input);
System.out.println("Total number of days " + input + " lies in month " + monthDigit + ".");
}
private static int extractMonthDigit (int totalNumberOfDays) {
int result = -1;
if(isInBetween(1, 31, totalNumberOfDays)) result = 0;
else if(isInBetween(32, 59, totalNumberOfDays)) result = 1;
else if(isInBetween(60, 90, totalNumberOfDays)) result = 2;
else if(isInBetween(91, 120, totalNumberOfDays)) result = 3;
else if(isInBetween(121, 151, totalNumberOfDays)) result = 4;
else if(isInBetween(152, 181, totalNumberOfDays)) result = 5;
else if(isInBetween(182, 212, totalNumberOfDays)) result = 6;
else if(isInBetween(213, 243, totalNumberOfDays)) result = 7;
else if(isInBetween(244, 274, totalNumberOfDays)) result = 8;
else if(isInBetween(275, 304, totalNumberOfDays)) result = 9;
else if(isInBetween(305, 335, totalNumberOfDays)) result = 10;
else if(isInBetween(336, 366, totalNumberOfDays)) result = 11;
return result;
}
private static boolean isInBetween(int min, int max, int days) {
boolean isInBetween = false;
if(days >= min && days <= max) {
isInBetween = true;
}
return isInBetween;
}
}
將不適用於閏年。 :) – Vishrant
試圖快速寫一個代碼,同意不考慮閏年,也只是印刷數量需要映射到實際月份等...感謝Vishrant。 –
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, dayOfYear_);
Date resultDate = calendar.getTime();
SimpleDateFormat stringFormat = new SimpleDateFormat("dd-MMM");
String result = stringFormat.format(resultDate);
- 1. 如何設置今天選擇的今年的默認年份?
- 2. 如何根據特定月份和今年的MySQL PHP獲取數據
- 3. 微觀數據的提供與根據今年
- 4. 如何根據迄今在SQL
- 5. SQL - 今年迄今爲止
- 6. 如何僅從今年的mysql數據庫獲取數據
- 7. 如何獲得單月和今年postgre
- 8. 如何根據年份顯示日期?
- 9. 如何做今年,基於今日()的MTD計算()
- 10. Django的 - 在今年
- 11. 設置今年之前 - 數據庫 - php
- 12. SQL在今年獲取數據
- 13. 熊貓據幀過濾:今天 - 今天+ 1年
- 14. 在今年變換時至今
- 15. Powerpivot:今年迄今爲止與去年同期相比
- 16. 當前年迄今爲止,去年迄今和其他
- 17. 如何查詢比較今年和去年的銷售額?
- 18. Excel - 如何根據「年齡月份」來計算年齡
- 19. 比較去年和今年的結果
- 20. DAX比較兩年本年迄今
- 21. 前幾年和今年的SQL YTD
- 22. 檢查今年是JavaScript的閏年
- 23. 倒計時直到今年年底 - JS
- 24. 試圖根據今天的日期確定長度(以月和年爲單位)
- 25. 根據今天,本週,本月,今年本季度總結銷售的最快方式?
- 26. 確定今年的一週
- 27. 添加2年迄今
- 28. 每月和列今年
- 29. DateTime今天25年後
- 30. Rails3中記錄了今年
你可以自己實現它 –
剛開始1月1日,並添加'DAYOFYEAR - 1'天... –
您還需要知道您是否處於閏年 – Henry