How can I round a date to the quarter start/end?
The zoo
package can help for many things date-related including this:
library(zoo)
as.yearqtr("2014-08-15", format="%Y-%m-%d")
## [1] "2014 Q3"
as.Date(as.yearqtr("2014-08-15", format="%Y-%m-%d"))
## [1] "2014-07-01"
But, that might not get you what you need (there are ways to extrapolate from those values).
The timeDate
package has:
timeFirstDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")
timeLastDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")
which might make it easer to use and tweak to adjust for different Q1 start origins.
Find next quarter end date given previous quarter end date using Java
To answer your question, I think you are looking for this :
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
LocalDate end = LocalDate.parse("30-09-20", formatter)
.plusMonths(3) // add three months to your date
.with(TemporalAdjusters.lastDayOfMonth()); // with the last day of the month
Note: don't use the legacy Date library, you tagged your question Java-8 which mean you can use java-time API.
Get last day of current quarter
@deHaar have reason, to get the end date of curent quarter, I would suggest to use :
public LocalDate lastDayFromDateQuarter(String date) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
LocalDate ld = LocalDate.parse(date, formatter);
int quarter = ld.get(IsoFields.QUARTER_OF_YEAR); // Get the Quarter, 1, 2, 3, 4
// Then create a new date with new quarter * 3 and last day of month
return ld.withMonth(quarter * 3).with(TemporalAdjusters.lastDayOfMonth());
}
Get last day of next quarter
To get the last day of the next quarter, then you just can add three months to your date like so :
public static LocalDate lastDayFromDateQuarter(String date) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
LocalDate ld = LocalDate.parse(date, formatter);
int quarter = ld.get(IsoFields.QUARTER_OF_YEAR);
return ld.withMonth(quarter * 3)
.plusMonths(3)
.with(TemporalAdjusters.lastDayOfMonth());
}
How to get the first date and last date of current quarter in java.util.Date
Here is solution in Java 7 or older (otherwise, I suggest to check other answers):
private static Date getFirstDayOfQuarter(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3);
return cal.getTime();
}
private static Date getLastDayOfQuarter(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3 + 2);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
}
Note 1: Months in java.util.Calendar are represented as integers starting from 0 (Jan) through 11 (Dec)
Note 2: division of integers in Java results in floor value (another integer value). So 2/3 = 0, 4/3 = 1 and so forth.
So, cal.get(Calendar.MONTH)/3 * 3
calculates zero-based index of quarter: 0(Q1), 1(Q2), 2(Q3), 3(Q4).
Example: Feb in Calendar is 1. So, 1 / 3 * 3 = 0.
If Feb date is supplied, then start of the quarter is 1st of Jan (because we got 0) and the end of quarter is last day of month 0 + 2 = 2 (Mar)
Get First Date and Last Date of Current Quarter in Python?
Any how i found some simple solution in c# and converted it into python,
from datetime import datetime,timedelta
current_date=datetime.now()
currQuarter = (current_date.month - 1) / 3 + 1
dtFirstDay = datetime(current_date.year, 3 * currQuarter - 2, 1)
dtLastDay = datetime(current_date.year, 3 * currQuarter + 1, 1) + timedelta(days=-1)
get startdate and enddate for current quarter php
check this for this quarter
.
case 'this_quarter':
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime('1-January-'.$current_year); // timestamp or 1-Januray 12:00:00 AM
$end_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM means end of 31 March
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM
$end_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM means end of 30 June
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM
$end_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM means end of 30 September
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM
$end_date = strtotime('1-January-'.($current_year+1)); // timestamp or 1-January Next year 12:00:00 AM means end of 31 December this year
}
break;
Update : 2
and for last quarter
case 'last_quarter':
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime('1-October-'.($current_year-1)); // timestamp or 1-October Last Year 12:00:00 AM
$end_date = strtotime('1-January-'.$current_year); // // timestamp or 1-January 12:00:00 AM means end of 31 December Last year
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime('1-January-'.$current_year); // timestamp or 1-Januray 12:00:00 AM
$end_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM means end of 31 March
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM
$end_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM means end of 30 June
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM
$end_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM means end of 30 September
}
break;
Related Topics
How to Save a Data Frame in a Txt or Excel File Separated by Columns
Ggplot2: Cannot Color Area Between Intersecting Lines Using Geom_Ribbon
How to Merge Multiple Data.Frames and Sum and Average Columns at the Same Time in R
What Is the "Embracing Operator" '{{ }}'
Creating a Grouped Bar Plot in R
Fastest Way to Do This Double Summation
Copying List of Files from One Folder to Other in R
Grid.Table and Tablegrob in Gridextra Package
How to Always Suppress Messages in R
Legend Venn Diagram in Venneuler
How to Change the Size of the Strip on Facets in a Ggplot
Roracle Not Working in R Studio
R Cmd Check Latex Error: Fatal PDFlatex - Gui Framework Cannot Be Initialized
R: Save All Data.Frames in Workspace to Separate .Rdata Files
Simple Comparing of Two Texts in R
In R: Joining Vector Elements by Row, Converting Vector Rows to Strings