Return today in date representation, but only containing year, month, and day. - Java java.time

Java examples for java.time:Month

Description

Return today in date representation, but only containing year, month, and day.

Demo Code


//package com.java2s;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;

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

public class Main {
    public static final int EPOCH_YEAR = 1970;
    public static final int EPOCH_MONTH = 1;
    public static final int EPOCH_DAY = 1;
    public static final long DAY_IN_MILIS = 86400000L;

    /**// w ww.jav a  2s .co m
     * Return today in date representation, but only containing year, month, and day.
     * @return today
     */
    public static Date getSimpleNow() {
        return getSimpleDate(getNow());
    }

    /**
     * Convert the full element date into simple element only containing year, month, and day.
     * @param date 
     * @return simpleDate simpler version of date
     */
    public static Date getSimpleDate(Date date) {
        int[] arrOfDate = createArrayOfDate(date);

        return getDate(arrOfDate[2], arrOfDate[1], arrOfDate[0]);
    }

    /**
     * Return today in date representation. Along with hour, minute, second, and other element.<br />
     * @return today
     */
    public static Date getNow() {
        return getCalendar().getTime();
    }

    /**
     * Extract date element (year, month, and date) into array of {@code int}.
     * @param date
     * @return arrOfDate array of date [DD, mm, YYYY]
     */
    public static int[] createArrayOfDate(Date date) {
        int[] arrOfDate = new int[3];

        arrOfDate[0] = getDay(date);
        arrOfDate[1] = getMonthInt(date);
        arrOfDate[2] = getYear(date);

        return arrOfDate;
    }

    /**
     * Create date object.
     * @param year {@code int}
     * @param month {@code int}
     * @param day {@code int}
     * @return date object
     */
    public static Date getDate(int year, int month, int day) {
        Calendar cal = getCalendar();
        cal.setTimeInMillis(toMilis(year, month, day));

        return cal.getTime();
    }

    /**
     * Create date from String value.<br />
     * Format of String is MM/dd/YYYY.
     * @param dateStr String value with format MM/dd/YYYY
     * @return date
     */
    public static Date getDate(String dateStr) {
        String delim = dateStr.contains("-") ? "-" : "/";

        return getDate(dateStr, delim);
    }

    /**
     * Create date from String value.<br />
     * Format of String is MM/dd/YYYY.
     * @param dateStr String value with format MM/dd/YYYY
     * @param delim
     * @return date
     */
    public static Date getDate(String dateStr, String delim) {
        String elStr[] = dateStr.split(delim);
        Date date = getDate(Integer.parseInt(elStr[2]),
                getMonthInt(elStr[0]), Integer.parseInt(elStr[1]));

        return date;
    }

    /**
     * Create date object.
     * @param year {@code int}
     * @param month {@code Month}
     * @param date {@code int}
     * @return
     */
    public static Date getDate(int year, Month month, int date) {
        return getDate(year, month.getValue(), date);
    }

    /**
     * Create default calendar.<br />
     * @return default calendar
     */
    public static Calendar getCalendar() {
        return Calendar.getInstance();
    }

    /**
     * Create calendar using date.<br />
     * @param date
     * @return calendar
     */
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        return cal;
    }

    /**
     * Return day in {@code int} representation from {@code Calendar}.
     * @param calendar
     * @return day in {@code int} representation
     */
    public static int getDay(Calendar calendar) {
        return calendar.get(Calendar.DATE);
    }

    /**
     * Return day in {@code int} representation from {@code Date}.
     * @param day
     * @return day in {@code int} representation
     */
    public static int getDay(Date date) {
        return getCalendar(date).get(Calendar.DATE);
    }

    /**
     * Return month in {@code int} representation from {@code Calendar}.<br />
     * @param calendar
     * @return month in {@code int} representation
     */
    public static int getMonthInt(Calendar calendar) {
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * Return month in {@code int} representation from {@code String}.
     * @param month
     * @return month in {@code int} representation
     */
    public static int getMonthInt(String month) {
        return Integer.parseInt(month);
    }

    /**
     * Return month in {@code int} representation from {@code Date}.
     * @param date
     * @return month in {@code int} representation
     */
    public static int getMonthInt(Date date) {
        return getMonthInt(getCalendar(date));
    }

    /**
     * Return year in {@code int} representation from {@code Calendar}.
     * @param calendar
     * @return year in {@code int} representation
     */
    public static int getYear(Calendar calendar) {
        return calendar.get(Calendar.YEAR);
    }

    /**
     * Return year in {@code int} representation from {@code Date}.
     * @param date
     * @return year in {@code int} representation
     */
    public static int getYear(Date date) {
        return getCalendar(date).get(Calendar.YEAR);
    }

    /**
     * Return date representation in millisecond.
     * @param date
     * @return date representation in millisecond.
     */
    public static long toMilis(Date date) {
        int year = getYear(date);
        int month = getMonthInt(date);
        int day = getDay(date);

        return toMilis(year, month, day);
    }

    /**
     * Return date representation in millisecond.
     * @param year
     * @param month
     * @param day
     * @return date representation in millisecond
     */
    public static long toMilis(int year, int month, int day) {
        LocalDate epoch = LocalDate.of(EPOCH_YEAR, EPOCH_MONTH, EPOCH_DAY);
        LocalDate created = LocalDate.of(year, month, day);

        long p = ChronoUnit.DAYS.between(epoch, created);

        return p * DAY_IN_MILIS;
    }
}

Related Tutorials