is Today - Java java.util

Java examples for java.util:Day

Description

is Today

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        long time = 2;
        System.out.println(isToday(time));
    }/*from w ww  .ja v a  2 s  .com*/

    public static boolean isToday(long time) {
        long now = getSystemTime();
        int[] nowFields = getTimeFields(now);
        int[] timeFields = getTimeFields(time);
        return nowFields[0] == timeFields[0]
                && nowFields[1] == timeFields[1]
                && nowFields[2] == timeFields[2];
    }

    public static boolean isToday(Calendar time) {
        long now = getSystemTime();
        int[] nowFields = getTimeFields(now);
        int[] timeFields = getTimeFields(time.getTimeInMillis());
        return nowFields[0] == timeFields[0]
                && nowFields[1] == timeFields[1]
                && nowFields[2] == timeFields[2];
    }

    public static long getSystemTime() {
        return System.currentTimeMillis();
    }

    public static int[] getTimeFields(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int[] timeFields = new int[6];
        timeFields[0] = calendar.get(Calendar.YEAR);
        timeFields[1] = calendar.get(Calendar.MONTH);
        timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH);
        timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY);
        timeFields[4] = calendar.get(Calendar.MINUTE);
        timeFields[5] = calendar.get(Calendar.SECOND);
        return timeFields;
    }
}

Related Tutorials