Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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.
 */

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

public class Main {
    /**
     * Packs the current time value into an MS-DOS time value.
     * @return the MS-DOS packed time
     */
    public static int packCurrentTime() {
        return packTime(new Date().getTime());
    }

    /**
     * Packs java time value into an MS-DOS time value.
     * @param time the time value
     * @return the MS-DOS packed time
     */
    public static int packTime(long time) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(time));

        int seconds = c.get(Calendar.SECOND);
        int minutes = c.get(Calendar.MINUTE);
        int hours = c.get(Calendar.HOUR_OF_DAY);

        /*
         * Here is how MS-DOS packs a time value:
         * 0-4: seconds (divided by 2 because we only have 5 bits = 32 different numbers)
         * 5-10: minutes (6 bits = 64 possible values)
         * 11-15: hours (5 bits = 32 possible values)
         *
         * source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247(v=vs.85).aspx
         */
        return (hours << 11) | (minutes << 5) | (seconds / 2);
    }
}