Java Date to Time getTime(Date timestamp)

Here you can find the source of getTime(Date timestamp)

Description

Return a time in Davis Vantage Pro format (two bytes).

License

Apache License

Parameter

Parameter Description
timestamp the time value

Return

a byte array of size 2

Declaration

public static byte[] getTime(Date timestamp) 

Method Source Code

//package com.java2s;
/*// w  w  w . j  a  v  a  2  s. co m
 *  Copyright 2006 Goran Ehrsson.
 *
 *  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.
 *  under the License.
 */

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

public class Main {
    /**
     * Return a time in Davis Vantage Pro format (two bytes). 
     * @param timestamp the time value
     * @return a byte array of size 2
     */
    public static byte[] getTime(Date timestamp) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(timestamp);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int value = hour * 100 + minute;
        return getBytes(value);
    }

    /**
     * Parse Vantage Pro date into <code>java.util.Date</code>.
     * 
     * @param buf station date format
     * @param offset byte offset where station time begins
     * @return the date in Java format.
     */
    public static Date getTime(byte[] buf, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.SECOND, (int) buf[offset + 0]);
        cal.set(Calendar.MINUTE, (int) buf[offset + 1]);
        cal.set(Calendar.HOUR_OF_DAY, (int) buf[offset + 2]);
        cal.set(Calendar.DAY_OF_MONTH, (int) buf[offset + 3]);
        cal.set(Calendar.MONTH, (int) buf[offset + 4] - 1);
        cal.set(Calendar.YEAR, (int) buf[offset + 5] + 1900);
        return cal.getTime();
    }

    /**
     * Return the two least significant bytes of a value.
     * The least signifcant byte is returned first (at index 0)
     * @param value the value to convert to bytes
     * @return a byte array of size 2 with LSB first.
     */
    public static byte[] getBytes(int value) {
        byte[] rval = new byte[2];
        // Least significant byte first.
        rval[0] = (byte) (value & 0xff);
        rval[1] = (byte) ((value >>> 8) & 0xff);
        return rval;
    }
}

Related

  1. getTime(Date date)
  2. getTime(Date date, boolean isEnd)
  3. getTime(Date date, String pattern, Locale locale)
  4. getTime(Date dateParam)
  5. getTime(Date executionTime)
  6. getTime(Date trialTime)
  7. getTime(final Date date, final int day)
  8. getTime(final Date time)
  9. getTime(java.util.Date date)