Question

I want a program in which I give month and year and the program should return start and end timestamp of the month.

For example, if I pass January and 2011 to the method it will return start=1293840000 end = 1296518400

Is there any way to do this.

Was it helpful?

Solution 2

You can do something like this

long startDate;
long endDate;

private void calculateMonthStartAndEndTime(int month, int year){
//create the first date of month
Calendar mycal = new GregorianCalendar(year,month, 1);
startDate = mycal.getTimeInMillis();

// Get the number of days in that month which actually gives the last date.
int lastDate = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
mycal = new GregorianCalendar(year, month, lastDate);
endDate = mycal.getTimeInMillis();
}

OTHER TIPS

Joda-Time

This kind of work is much easier using the Joda-Time library.

Time Zone

You should specify a time zone, rather than rely on default of the JVM/host computer. Seems that you want UTC (no time zone offset), so your code should say so explicitly.

Half-Open

Also, getting the last moment of a day is tricky. In theory, there is always another divisible fraction of second before the start of the next day. While you want to resolve to seconds (Unix item), two of the common Java libraries (java.util.Date/.Calendar and Joda-Time) use milliseconds, and the new java.time library in Java 8 resolves to nanoseconds. Instead, date-time work is often done using the "half-open" approach where a span of time is defined with the beginning to be inclusive and the ending to be exclusive. So "January" runs from first moment of January 1 to first moment of February 1.

Example

Here is some example code in Joda-Time 2.3.

DateTimeZone timeZone = DateTimeZone.UTC;
DateTime start = new DateTime( 2011, DateTimeConstants.JANUARY, 1, 0, 0, 0, timeZone).withTimeAtStartOfDay();
DateTime stop = start.plusMonths( 1 );

You might find the Interval class interesting for related work.

Interval january2011 = new Interval( start, stop ); 

Convert to seconds, from milliseconds.

long secondsStart = start.getMillis()/1000L;
long secondsStop = stop.getMillis()/1000L;

Update with the Java 8 answer

(Java 8 includes a new time library based on JodaTime):

    ZoneId timeZone = ZoneId.of("US/Eastern");
    //ZoneId timeZone = ZoneOffset.UTC;

    LocalDate march1985 = LocalDate.of(2017, Month.MARCH, 1);
    long beginningOfMarch = march1985.atStartOfDay(timeZone)
            .toInstant()
            .toEpochMilli();

    LocalDate april1985 = march1985.plus(1, ChronoUnit.MONTHS);
    long endOfMarch = april1985.atStartOfDay(timeZone)
            .toInstant()
            .toEpochMilli();

Time Zones

The "start of the month" is a function of the time zone, so in this case I've shown how to do it in Eastern time. These java.time classes handle DST automatically, so, for example, if you do the year 2017 instead of 1985, you'll find the difference is 743 hours instead of 744, because in 1985 DST took place in April.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top