Java Milliseconds millisecondOf(Date date)

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

Description

Extracts the millisecond of the given Date.

License

Apache License

Parameter

Parameter Description
date the date to extract the millisecond from - must not be null.

Exception

Parameter Description
NullPointerException if given Date is null

Return

the millisecond of the given Date

Declaration

public static int millisecondOf(Date date) 

Method Source Code

//package com.java2s;
/**//ww  w  .  j  av a  2  s  . c om
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */

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

public class Main {
    /**
     * Extracts the millisecond of the given Date.
     *
     * @param date the date to extract the millisecond from - must not be null.
     * @return the millisecond of the given Date
     * @throws NullPointerException if given Date is null
     */
    public static int millisecondOf(Date date) {
        return toCalendar(date).get(Calendar.MILLISECOND);
    }

    /**
     * Converts the given Date to Calendar, returns null if the given Date is null.
     *
     * @param date the date to convert to a Calendar.
     * @return the Calendar corresponding to the given Date or null if the given Date is null.
     */
    public static Calendar toCalendar(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. microsToMillis(long micros)
  2. microsToMillis(long sec, long usec)
  3. milliDate(String date)
  4. millisBetween(Date firstDate, Date lastDate)
  5. millisecond(final Date date)
  6. milliseconds(int offset)
  7. millisecondsBetween(Date start, Date finish)
  8. millisecondsSinceMidnight(Date date)
  9. millisToDate(String stime)