Java Calendar Calculate getLongTime(Calendar cal)

Here you can find the source of getLongTime(Calendar cal)

Description

get Long Time

License

Apache License

Declaration

public static long getLongTime(Calendar cal) 

Method Source Code

//package com.java2s;
/*//from  w w  w  .  j  ava 2  s. c om
* Copyright 2009-2011 Telkku.com
*
* 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.*;

public class Main {
    public static long getLongTime(Calendar cal) {
        int date = getDateInt(cal);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        String hourStr = addPreZero(hour);
        String minuteStr = addPreZero(minute);

        String time = date + hourStr + minuteStr;

        return Long.parseLong(time);
    }

    /**
     * Returns today's date as an integer
     * eg.: 06-06-2008 => 20080606
     * */
    public static int getDateInt() {
        return getDateInt(0);
    }

    /**
     * Returns chosen date as an integer
     * @param advance - how many days to advance from current date (+/-)
     * @return Date formatted as an integer
     * */
    public static int getDateInt(int advance) {
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_MONTH, advance);

        return getDateInt(cal);
    }

    public static int getDateInt(int advance, int timezone) {
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_MONTH, advance);
        cal.add(Calendar.HOUR_OF_DAY, -timezone);

        return getDateInt(cal);
    }

    /**
     * Returns the chosen date as an integer.
     * @param cal - a <code>Calendar</code> that is set to the chosen date
     * @return The chosen date formatted as an integer
     * */
    public static int getDateInt(Calendar cal) {
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String dayString = addPreZero(day);

        int month = cal.get(Calendar.MONTH) + 1;
        String monthString = addPreZero(month);

        String date = "" + cal.get(Calendar.YEAR) + monthString + dayString;
        return Integer.parseInt(date);
    }

    /**
     * Adds a zero (0) to the string if the given value is below ten (10).
     * */
    private static String addPreZero(int value) {
        if (value < 10)
            return "0" + value;
        else
            return "" + value;
    }
}

Related

  1. encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf)
  2. equals(Calendar calendar1, Calendar calendar2)
  3. FormatTime(Calendar p_time, boolean p_showMilliseconds)
  4. getCalendar(long millis)
  5. getGregorianCalendar(String yyyymmdd)
  6. getTimeStr3(Calendar timeInstance)
  7. getTimeString(Calendar cal)
  8. getTimeStrWithMillisecond(Calendar timeInstance)
  9. isAfterWorkTime(Calendar start)