Given a java.util.Date object how do I go about finding what Quarter it's in?
给定一个java.util.Date对象,我该如何查找它所在的Quarter?
Assuming Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.
假设Q1 = 1月2月3月,Q2 = 4月,5月,6月等。
13 个解决方案
#1
45
You could use
你可以用
int quarter = (myDate.getMonth() / 3) + 1;
Be warned, though that getMonth is deprecated:
请注意,虽然getMonth已被弃用:
As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).
从JDK 1.1版开始,由Calendar.get(Calendar.MONTH)取代。
#2
#3
12
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:
您将不得不编写自己的代码,因为每个业务的术语“Quarter”是不同的。你不能只做这样的事情:
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";
#4
10
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.
这将避免在非西方日历上获得第13个月(Calendar.UNDECIMBER),以及由于较短的月份而导致的任何偏差。
#5
4
If you have
如果你有
private static final int[] quarters = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
Then current quarter
is
然后是当前季度
private static final int thisQuarter = quarters[thisMonth];
Where thisMonth
is
这个月是哪里
private static final int thisMonth = cal.get(Calendar.MONTH);
#6
3
When using Joda time, use Math.ceil() function:
使用Joda时,请使用Math.ceil()函数:
double quarter = Math.ceil(new Double(jodaDate.getMonthOfYear()) / 3.0);
#7
2
Make sure that the thisMonth
is at least a float or a double, not an int:
确保thisMonth至少是float或double,而不是int:
String quarter = thisMonth/3 <= 1 ? "Q1" : thisMonth/3 <= 2 ? "Q2" : thisMonth/3 <= 3 ? "Q3" : "Q4";
Regards, MS
#8
1
JFreeChart has a Quarter class. If you're curious, check out the javadoc. The source is also available from SourceForge if you want to check out the implementation.
JFreeChart有一个Quarter类。如果你很好奇,请查看javadoc。如果您想查看实现,也可以从SourceForge获取源代码。
#9
1
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.
您可能希望扩展或封装日历类以将其自定义为您的任务,而不是编写一些转换它的实用程序函数。您的应用程序在该领域可能非常复杂,您可以在其中找到新日历类的其他用途 - 我保证您会很高兴扩展或封装它,即使它现在看起来很愚蠢。
#10
1
int month = Calendar.getInstance().get( Calendar.MONTH ) + 1;
int quarter = month % 3 == 0? (month / 3): ( month / 3)+1;
#11
1
tl;dr
YearQuarter
.from(
LocalDate.of( 2018 , 1 , 23 )
)
ThreeTen-Extra
The Answer by abdmob is correct: Using java.time is the wise way to go.
abdmob的回答是正确的:使用java.time是明智之举。
In addition, the ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.
此外,ThreeTen-Extra项目还使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。
Its useful classes include:
其有用的课程包括:
Current quarter
To get the current quarter, specify a time zone. Determining a quarter means determining a date. And determining a date means specifying a time zone. For any given moment, the date varies around the globe by zone. Specify by using a proper time zone name in form of continent/region
. Never use the 3-4 letter abbreviations such as EST
or IST
.
要获取当前季度,请指定时区。确定季度意味着确定日期。确定日期意味着指定时区。对于任何给定的时刻,日期在全球范围内因地区而异。通过使用大陆/地区形式的适当时区名称进行指定。切勿使用3-4字母缩写,例如EST或IST。
ZoneId z = ZoneId.of( "America/Montreal" );
YearQuarter yq = YearQuarter.now( z );
Given a java.util.Date
If given a java.util.Date
first convert to a java.time type. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
如果给定java.util.Date,则首先转换为java.time类型。 Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒。
Instant instant = myUtilDate.toInstant();
Assign a time zone to get a ZoneDateTime
.
分配时区以获取ZoneDateTime。
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Generate a YearQuarter
from that ZonedDateTime
.
从ZonedDateTime生成YearQuarter。
YearQuarter yq = YearQuarter.from( zdt );
Use throughout your code
You should be passing around instances of YearQuarter
rather than mere numbers or strings. Using objects provides type-safety, ensures valid values, and makes your code more self-documenting.
你应该传递YearQuarter的实例,而不仅仅是数字或字符串。使用对象提供类型安全性,确保有效值,并使您的代码更加自我记录。
#12
0
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];
#13
0
with java8 you may use formatter:
使用java8你可以使用formatter:
import java.time.format.DateTimeFormatter
import java.time.LocalDate
println("withQuarter: " + LocalDate.of("2016".toInt,"07".toInt,1).format(DateTimeFormatter.ofPattern("yyyyQMM")))
withQuarter: 2016307
#1
45
You could use
你可以用
int quarter = (myDate.getMonth() / 3) + 1;
Be warned, though that getMonth is deprecated:
请注意,虽然getMonth已被弃用:
As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).
从JDK 1.1版开始,由Calendar.get(Calendar.MONTH)取代。
#2
22
In Java 8 and later, the java.time classes have a more simple version of it. Use LocalDate
and IsoFields
在Java 8及更高版本中,java.time类具有更简单的版本。使用LocalDate和IsoFields
LocalDate.now().get(IsoFields.QUARTER_OF_YEAR)
#3
12
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:
您将不得不编写自己的代码,因为每个业务的术语“Quarter”是不同的。你不能只做这样的事情:
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";
#4
10
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.
这将避免在非西方日历上获得第13个月(Calendar.UNDECIMBER),以及由于较短的月份而导致的任何偏差。
#5
4
If you have
如果你有
private static final int[] quarters = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
Then current quarter
is
然后是当前季度
private static final int thisQuarter = quarters[thisMonth];
Where thisMonth
is
这个月是哪里
private static final int thisMonth = cal.get(Calendar.MONTH);
#6
3
When using Joda time, use Math.ceil() function:
使用Joda时,请使用Math.ceil()函数:
double quarter = Math.ceil(new Double(jodaDate.getMonthOfYear()) / 3.0);
#7
2
Make sure that the thisMonth
is at least a float or a double, not an int:
确保thisMonth至少是float或double,而不是int:
String quarter = thisMonth/3 <= 1 ? "Q1" : thisMonth/3 <= 2 ? "Q2" : thisMonth/3 <= 3 ? "Q3" : "Q4";
Regards, MS
#8
1
JFreeChart has a Quarter class. If you're curious, check out the javadoc. The source is also available from SourceForge if you want to check out the implementation.
JFreeChart有一个Quarter类。如果你很好奇,请查看javadoc。如果您想查看实现,也可以从SourceForge获取源代码。
#9
1
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.
您可能希望扩展或封装日历类以将其自定义为您的任务,而不是编写一些转换它的实用程序函数。您的应用程序在该领域可能非常复杂,您可以在其中找到新日历类的其他用途 - 我保证您会很高兴扩展或封装它,即使它现在看起来很愚蠢。
#10
1
int month = Calendar.getInstance().get( Calendar.MONTH ) + 1;
int quarter = month % 3 == 0? (month / 3): ( month / 3)+1;
#11
1
tl;dr
YearQuarter
.from(
LocalDate.of( 2018 , 1 , 23 )
)
ThreeTen-Extra
The Answer by abdmob is correct: Using java.time is the wise way to go.
abdmob的回答是正确的:使用java.time是明智之举。
In addition, the ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.
此外,ThreeTen-Extra项目还使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。
Its useful classes include:
其有用的课程包括:
Current quarter
To get the current quarter, specify a time zone. Determining a quarter means determining a date. And determining a date means specifying a time zone. For any given moment, the date varies around the globe by zone. Specify by using a proper time zone name in form of continent/region
. Never use the 3-4 letter abbreviations such as EST
or IST
.
要获取当前季度,请指定时区。确定季度意味着确定日期。确定日期意味着指定时区。对于任何给定的时刻,日期在全球范围内因地区而异。通过使用大陆/地区形式的适当时区名称进行指定。切勿使用3-4字母缩写,例如EST或IST。
ZoneId z = ZoneId.of( "America/Montreal" );
YearQuarter yq = YearQuarter.now( z );
Given a java.util.Date
If given a java.util.Date
first convert to a java.time type. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
如果给定java.util.Date,则首先转换为java.time类型。 Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒。
Instant instant = myUtilDate.toInstant();
Assign a time zone to get a ZoneDateTime
.
分配时区以获取ZoneDateTime。
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Generate a YearQuarter
from that ZonedDateTime
.
从ZonedDateTime生成YearQuarter。
YearQuarter yq = YearQuarter.from( zdt );
Use throughout your code
You should be passing around instances of YearQuarter
rather than mere numbers or strings. Using objects provides type-safety, ensures valid values, and makes your code more self-documenting.
你应该传递YearQuarter的实例,而不仅仅是数字或字符串。使用对象提供类型安全性,确保有效值,并使您的代码更加自我记录。
#12
0
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];
#13
0
with java8 you may use formatter:
使用java8你可以使用formatter:
import java.time.format.DateTimeFormatter
import java.time.LocalDate
println("withQuarter: " + LocalDate.of("2016".toInt,"07".toInt,1).format(DateTimeFormatter.ofPattern("yyyyQMM")))
withQuarter: 2016307