In accordance with mysql function to_days - Java java.sql

Java examples for java.sql:MySQL

Description

In accordance with mysql function to_days

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from w ww  .ja  v  a  2  s.c om
     * days elapsed since 1970-01-01, mysql to_days('1970-01-01')
     */
    private static final int BASE_DAYS_OFFSET = 719528;

    /**
     * In accordance with mysql function to_days
     *
     * @param date
     * @return
     */
    public static int toDays(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND,
                cal.get(Calendar.ZONE_OFFSET)
                        + cal.get(Calendar.DST_OFFSET));

        return (int) (cal.getTimeInMillis() / 1000 / 60 / 60 / 24)
                + BASE_DAYS_OFFSET;
    }
}

Related Tutorials