Java Milliseconds unixToDosTime(long timeMillis)

Here you can find the source of unixToDosTime(long timeMillis)

Description

Converts a unix timestamp into a 32-bit DOS timestamp.

License

Open Source License

Declaration

static int unixToDosTime(long timeMillis) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from  ww w  .j  a  va 2 s  .c  o  m
     * Midnight Jan 1st 1980. Uses the current time zone as the DOS format does not support time zones
     * and will always assume the current zone.
     */
    public static final long DOS_EPOCH = new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0)
            .getTimeInMillis();
    /** 23:59:59 Dec 31st 2107. The maximum date representable in DOS format. */
    public static final long MAX_DOS_DATE = new GregorianCalendar(2107, Calendar.DECEMBER, 31, 23, 59, 59)
            .getTimeInMillis();
    private static final int DOS_MINUTE_OFFSET = 5;
    private static final int DOS_HOUR_OFFSET = 11;
    private static final int DOS_DAY_OFFSET = 16;
    private static final int DOS_MONTH_OFFSET = 21;
    private static final int DOS_YEAR_OFFSET = 25;

    /** Converts a unix timestamp into a 32-bit DOS timestamp. */
    static int unixToDosTime(long timeMillis) {
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(timeMillis);

        if (!isValidInDos(timeMillis)) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            throw new IllegalArgumentException(String.format(
                    "%s is not representable in the DOS time" + " format. It must be in the range %s to %s",
                    df.format(time.getTime()), df.format(new Date(DOS_EPOCH)), df.format(new Date(MAX_DOS_DATE))));
        }

        int dos = time.get(Calendar.SECOND) / 2;
        dos |= time.get(Calendar.MINUTE) << DOS_MINUTE_OFFSET;
        dos |= time.get(Calendar.HOUR_OF_DAY) << DOS_HOUR_OFFSET;
        dos |= time.get(Calendar.DAY_OF_MONTH) << DOS_DAY_OFFSET;
        dos |= (time.get(Calendar.MONTH) + 1) << DOS_MONTH_OFFSET;
        dos |= (time.get(Calendar.YEAR) - 1980) << DOS_YEAR_OFFSET;
        return dos;
    }

    /** Checks if the unix timestamp is representable as a valid DOS timestamp.
     *
     * <p>See <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">ZIP Format</a> for
     * a general description of the date a time fields (Section 4.4.6) and
     * <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247.aspx">DOS date
     * format</a> for a detailed description of the format.
     */
    static boolean isValidInDos(long timeMillis) {
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(timeMillis);
        Calendar minTime = Calendar.getInstance();
        minTime.setTimeInMillis(DOS_EPOCH);
        Calendar maxTime = Calendar.getInstance();
        maxTime.setTimeInMillis(MAX_DOS_DATE);
        return (!time.before(minTime) && !time.after(maxTime));
    }
}

Related

  1. toSpacelessDateString(long millisSinceEpoch)
  2. ToSting(Long milliSecond)
  3. toTime(long milli)
  4. truncateMillisecond(Date date)
  5. trySleepMillis(int millis)
  6. waitMillis(long millis)
  7. waitMillis(long time)
  8. waitMilliSeconds(final long waitTimeInMilliseconds)
  9. waitMilliSeconds(int ms)