Java Timestamp timestampIntToTimestamp(long timestampInt)

Here you can find the source of timestampIntToTimestamp(long timestampInt)

Description

Converts timestamp to Timestamp-object

License

Apache License

Parameter

Parameter Description
timestampInt - timestamp in integer formatted form

Return

Timestamp or null if input is invalid

Declaration

public static Timestamp timestampIntToTimestamp(long timestampInt) 

Method Source Code


//package com.java2s;
/*//from w w  w  .j  av  a  2s.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.sql.Timestamp;

import java.util.*;

public class Main {
    /**
     * Converts timestamp to <code>Timestamp</code>-object
     * @param timestampInt - timestamp in integer formatted form
     * @return <code>Timestamp</code> or null if input is invalid
     * */
    public static Timestamp timestampIntToTimestamp(long timestampInt) {
        return timestampIntToTimestamp(String.valueOf(timestampInt));
    }

    /**
     * Converts timestamp to <code>Timestamp</code>-object
     * @param timestampInt - timestamp in integer formatted form
     * @return <code>Timestamp</code> or null if input is invalid
     * */
    public static Timestamp timestampIntToTimestamp(String timestampInt) {
        if (timestampInt.length() == 12) {
            String date = timestampInt.substring(0, 8);
            String time = timestampInt.substring(8, 12);

            String hourString = "" + time.charAt(0) + time.charAt(1);
            String minuteString = "" + time.charAt(2) + time.charAt(3);

            Calendar calendar = dateIntToCalendar(date);
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourString));
            calendar.set(Calendar.MINUTE, Integer.parseInt(minuteString));

            return new Timestamp(calendar.getTimeInMillis());
        } else {
            return null;
        }
    }

    /**
     * Converts an integer formatted date to an instance of <code>Calendar</code>
     * @param dateInt - integer formatted date
     * @return An instance of <code>Calendar</code> set to the date contained in dateInt.
     * */
    public static Calendar dateIntToCalendar(int dateInt) {
        return dateIntToCalendar(String.valueOf(dateInt));
    }

    /**
     * Converts an integer formatted date to an instance of <code>Calendar</code>
     * @param dateInt - integer formatted date as a String
     * @return An instance of <code>Calendar</code> set to the date contained in dateInt.
     * */
    public static Calendar dateIntToCalendar(String dateInt) {
        if (dateInt.length() == 8) {
            String yearString = "" + dateInt.charAt(0) + dateInt.charAt(1) + dateInt.charAt(2) + dateInt.charAt(3);
            String monthString = "" + dateInt.charAt(4) + dateInt.charAt(5);
            String dayString = "" + dateInt.charAt(6) + dateInt.charAt(7);

            Calendar cal = new GregorianCalendar();
            cal.set(Calendar.YEAR, Integer.parseInt(yearString));
            cal.set(Calendar.MONTH, Integer.parseInt(monthString) - 1);//Months in java.util.Calendar start from zero(0) 
            cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dayString));

            return cal;
        } else {
            return null;
        }
    }
}

Related

  1. StrToTimestamp(String timestampStr,String pattern)
  2. timestamp()
  3. timestamp(int year, int month, int day, int hour, int minute, int second, int nanosecond)
  4. timestamp(Long param)
  5. timestampInMs()
  6. timestampIsEquals(Timestamp timestamp1, Timestamp timestamp2)
  7. TimeStampMonthDayYear(Timestamp timestamp)
  8. TimestampNow()
  9. timestampNow()