get Current Quarter End Times - Java java.util

Java examples for java.util:Quarter

Description

get Current Quarter End Times

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getCurrentQuarterEndTimes());
    }//from www  .  ja v a  2  s  .  c  o  m

    private static TimeZone timeZoneChina = TimeZone
            .getTimeZone("Asia/Shanghai");

    public static Date getCurrentQuarterEndTimes() {
        SimpleDateFormat longSdf = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = getSimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1); //?
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return now;
    }

    public static SimpleDateFormat getSimpleDateFormat(String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        sdf.setTimeZone(timeZoneChina);
        return sdf;
    }
}

Related Tutorials