本文介绍了使用Java识别从Date开始的季节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有过好运,所以为什么不再试一次?

I've had nothing but good luck from SO, so why not try again?

我有一个应用程序需要根据季节显示不同的图像(春,夏,冬,秋)。我有这些季节的非常具体的开始和结束日期。

I have an application that needs to show a different image based on the season of the year (spring, summer, winter, fall). I have very specific start and end dates for these seasons.

我希望你的天才是一种称为GetSeason的方法,它将日期作为输入并返回一个String值春天,夏天,冬天或秋天。以下是日期范围及其相关季节:

What I would like from you geniuses is a method called GetSeason that takes a date as input and returns a String value of Spring, Summer, Winter or Fall. Here are the date ranges and their associated seasons:

春季:3 / 1-4 / 30

夏季:5 / 1- 8/31

秋季:9 / 1-10 / 31

冬季:11 / 1-2 / 28

Spring:3/1-4/30
Summer:5/1-8/31
Fall:9/1-10/31
Winter: 11/1-2/28

有人可以提供一种恢复正常季节的工作方法吗?
感谢大家!

Can someone provide a working method to return the proper season?Thanks everyone!

推荐答案

似乎只是检查月份会做的:

Seems like just checking the month would do:

private static final String seasons[] = {
    "Winter", "Winter",
    "Spring", "Spring", "Spring",
    "Summer", "Summer", "Summer",
    "Fall", "Fall", "Fall",
    "Winter"
};
public String getSeason( Date date ) {
   return seasons[ date.getMonth() ];
}

// As stated above, getMonth() is deprecated, but if you start with a Date, 
// you'd have to convert to Calendar before continuing with new Java, 
// and that's not fast.

这篇关于使用Java识别从Date开始的季节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:53