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
How do I discover the Quarter of a given Date?
提问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
回答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 quarter
is
那么电流quarter
是
private static final int thisQuarter = quarters[thisMonth];
Where thisMonth
is
哪里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 thisMonth
is 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];