Java 如何发现给定日期的季度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:51:50  来源:igfitidea点击:

How do I discover the Quarter of a given Date?

javacalendar

提问by Allain Lalonde

Given a java.util.Date object how do I go about finding what Quarter it's in?

给定一个 java.util.Date 对象,我该如何查找它所在的季度?

Assuming Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.

假设 Q1 = Jan Feb Mar,Q2 = Apr、May、Jun 等。

采纳答案by Bill the Lizard

You could use

你可以用

int quarter = (myDate.getMonth() / 3) + 1;

Be warned, though that getMonthis deprecated:

请注意,尽管不推荐使用getMonth

As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

从 JDK 1.1 版开始,由 Calendar.get(Calendar.MONTH) 取代。

回答by Outlaw Programmer

You are going to have to write your own code because the term "Quarter" is different for each business. Can't you just do something like:

您将不得不编写自己的代码,因为每个业务的“季度”一词都不同。你不能做这样的事情:

Calendar c = /* get from somewhere */
int month = c.get(Calendar.MONTH);

return (month >= Calendar.JANUARY && month <= Calendar.MARCH)     ? "Q1" :
       (month >= Calendar.APRIL && month <= Calendar.JUNE)        ? "Q2" :
       (month >= Calendar.JULY && month <= Calendar.SEPTEMBER)    ? "Q3" :
                                                                    "Q4";

回答by erickson

Since quarters are a localized (Western) concept, specify a Locale rather than using the platform default:

由于宿舍是本地化(西方)概念,请指定语言环境而不是使用平台默认值:

Calendar cal = Calendar.getInstance(Locale.US);
/* Consider whether you need to set the calendar's timezone. */
cal.setTime(date);
int month = cal.get(Calendar.MONTH); /* 0 through 11 */
int quarter = (month / 3) + 1;

This will avoid getting the thirteenth month (Calendar.UNDECIMBER) on non-Western calendars, and any skew caused by their shorter months.

这将避免在非西方日历上获得第十三个月 (Calendar.UNDECIMBER),以及由于它们较短的月份而导致的任何偏差。

回答by Brendan Cashman

JFreeChart has a Quarter class. If you're curious, check out the javadoc. The source is also available from SourceForgeif you want to check out the implementation.

JFreeChart 有一个 Quarter 类。如果您好奇,请查看javadoc。如果您想查看实现,也可以从SourceForge获得源代码。

回答by Bill K

Good solutions here, but remember that quarters can be subject to change depending on company/industry too. Sometimes a quarter can be a different 3 months.

这里有很好的解决方案,但请记住,季度也可能因公司/行业而异。有时一个季度可能是不同的 3 个月。

You probably want to extend or encapsulate the calendar class to customize it to your tasks rather than write some utility function that converts it. Your application is probably complex enough in that area that you will find other uses for your new calendar class--I promise you'll be glad you extended or encapsulated it even if it seems silly now.

您可能希望扩展或封装日历类以根据您的任务自定义它,而不是编写一些实用程序函数来转换它。您的应用程序在该领域可能足够复杂,您会发现新日历类的其他用途——我保证您会很高兴扩展或封装它,即使现在看起来很傻。

回答by James Raitsev

If you have

如果你有

private static final int[] quarters = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};

Then current quarteris

那么电流quarter

private static final int thisQuarter = quarters[thisMonth];

Where thisMonthis

哪里thisMonth

private static final int thisMonth = cal.get(Calendar.MONTH);

回答by user3830848

int month = Calendar.getInstance().get( Calendar.MONTH ) + 1;

int quarter = month % 3 == 0?  (month / 3): ( month / 3)+1;

回答by Marco

Make sure that the thisMonthis at least a float or a double, not an int:

确保thisMonth至少是浮点数或双精度数,而不是整数:

String quarter = thisMonth/3 <= 1 ? "Q1" : thisMonth/3 <= 2 ? "Q2" : thisMonth/3 <= 3 ? "Q3" : "Q4";

Regards, MS

问候,女士

回答by Keiichi

For Me, I Used this method for string representation:

对我来说,我用这个方法来表示字符串:

int quarter = (Calendar.getInstance().get(Calendar.MONTH) / 3); // 0 to 3
String[] mQuarterKey = {"qt1", "qt2", "qt3", "qt4"};
String strQuarter = mQuarterKey[quarter];

回答by abdolence

In Java 8 and later, the java.timeclasses have a more simple version of it. Use LocalDateand IsoFields

在 Java 8 及更高版本中,java.time类有一个更简单的版本。使用LocalDateIsoFields

LocalDate.now().get(IsoFields.QUARTER_OF_YEAR)