Java Date to Day getDayShort(Date date)

Here you can find the source of getDayShort(Date date)

Description

get Day Short

License

Open Source License

Declaration

public static int getDayShort(Date date) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class Main {
    public static final long START_2011 = 1293876000000l;
    public static HashMap<Integer, Integer> DAYS_2011 = new HashMap<Integer, Integer>();

    public static int getDayShort(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(START_2011);

        Calendar targetCalendar = Calendar.getInstance();
        targetCalendar.setTime(date);/*  w w  w.jav a  2s.c  om*/
        targetCalendar.set(Calendar.HOUR_OF_DAY, 0);
        targetCalendar.set(Calendar.MINUTE, 0);
        targetCalendar.set(Calendar.SECOND, 0);
        targetCalendar.set(Calendar.MILLISECOND, 0);

        int daysBetween = 0;
        int targetYear = targetCalendar.get(Calendar.YEAR);
        if (calendar.get(Calendar.YEAR) <= targetYear) {
            if (DAYS_2011.containsKey(targetYear)) {
                daysBetween += DAYS_2011.get(targetYear);
                calendar.set(Calendar.YEAR, targetYear);
            }
        }
        while (calendar.before(targetCalendar)) {
            //todo optimize;
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return 1 + daysBetween;
    }

    /**
     * @return day from beginning of 2011 Year
     *         It's NOT the same as ((extract(year from NOW()) - 2011)*365 + extract(doy from NOW())) in database;
     *         As leap years counting.
     */
    public static int getDayShort() {
        return getDayShort(Calendar.getInstance().getTime());
    }
}

Related

  1. getDayOfDate(Date date)
  2. getDayOfThisYear(Date currentdate)
  3. getDayOfYear(Date date)
  4. getDays(Date t, Date baseDate)
  5. getDaysEarlierDate(int days)
  6. getDaysMin(Date d)
  7. getDaysPassedSince(Date dateLastModified)
  8. getDayStart(Date date)
  9. getDayStart(final Date date)