Here you can find the source of timestampIntToTimestamp(long timestampInt)
Timestamp
-object
Parameter | Description |
---|---|
timestampInt | - timestamp in integer formatted form |
Timestamp
or null if input is invalid
public static Timestamp timestampIntToTimestamp(long timestampInt)
//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; } } }