Java Timestamp dateIntToTimestamp(int dateInt)

Here you can find the source of dateIntToTimestamp(int dateInt)

Description

Converts an integer formatted to an instance of Timestamp

License

Apache License

Parameter

Parameter Description
dateInt - integer formatted date

Return

An instance of Timestamp set to the date contained in dateInt-parameter

Declaration

public static Timestamp dateIntToTimestamp(int dateInt) 

Method Source Code


//package com.java2s;
/*//w ww  .  j  a  v  a 2 s. c  o m
* 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 an integer formatted to an instance of <code>Timestamp</code>
     * @param dateInt - integer formatted date
     * @return An instance of <code>Timestamp</code> set to the date contained in dateInt-parameter
     * */
    public static Timestamp dateIntToTimestamp(int dateInt) {
        Calendar calendar = dateIntToCalendar(dateInt);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 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. createDateTimestamp(java.util.Date date)
  2. date2Timestamp(Date date)
  3. date2Timestamp(Date value)
  4. dateAdd(Timestamp t1, Integer i, int type)
  5. dateAndTimeToTimestamp(Date data, Time hora)
  6. datesDiffer(Timestamp a, Timestamp b)
  7. dateStringToTimestamp(String dateStr)
  8. datetime2ToTimestamp(long value, int fraction, int width)
  9. dateTimeToString(Timestamp dt, String df)