Java Timestamp Create getTimestamp(String date, String time)

Here you can find the source of getTimestamp(String date, String time)

Description

get Timestamp

License

Open Source License

Parameter

Parameter Description
date a parameter
time a parameter

Declaration

public static Timestamp getTimestamp(String date, String time) 

Method Source Code

//package com.java2s;
/*/*from ww  w .  j  ava  2  s.  c  om*/
 * Created on Oct 23, 2003
 *
 * Copyright (c) 2005 Derone Bryson
 *
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this software; see the file COPYING. If not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * As a special exception, Derone Bryson and the StopMojo Project gives 
 * permission for additional uses of the text contained in its release of 
 * StopMojo.
 *
 * The exception is that, Derone Bryson and the the StopMojo Project hereby 
 * grants permission for non-GPL compatible modules (jar files, libraries, 
 * codecs, etc.) to be used and distributed together with StopMojo. This 
 * permission is above and beyond the permissions granted by the GPL license 
 * StopMojo is covered by.
 *
 * This exception does not however invalidate any other reasons why the 
 * executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by Derone Bryson and/or the
 * StopMojo Project under the name StopMojo. If you copy code from other Free 
 * Software Foundation releases into a copy of StopMojo, as the General Public 
 * License permits, the exception does not apply to the code that you add in 
 * this way. To avoid misleading anyone as to the status of such modified files, 
 * you must delete this exception notice from them.
 *
 * If you write modifications of your own for StopMojo, it is your choice 
 * whether to permit this exception to apply to your modifications. If you do 
 * not wish that, delete this exception notice.  
 */

import java.sql.Timestamp;

import java.util.Calendar;

public class Main {
    /**
     * 
     * @param date
     * @param time
     * @return
     */
    public static Timestamp getTimestamp(String date, String time) {
        Calendar c = dateToCalendar(date);

        // System.out.println("time = " + time + ", " + time.substring(0, 2) +
        // ", " + time.substring(2, 4));
        c.set(Calendar.HOUR_OF_DAY, atoi(time.substring(0, 2)));
        c.set(Calendar.MINUTE, atoi(time.substring(2, 4)));
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return new Timestamp(c.getTimeInMillis());
    }

    /**
     * 
     * @param date
     * @return
     */
    public static Calendar dateToCalendar(String date) {
        int y, m, d;

        Calendar c = Calendar.getInstance();

        y = atoi(date.substring(6));
        if (y < 49)
            y += 2000;
        else
            y += 1900;
        d = atoi(date.substring(3));
        m = atoi(date) - 1;

        c.set(y, m, d, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    /**
     * Returns the integer value of s.
     * 
     * @param s
     *            string containing the integer
     * @return the value of the integer in s
     */
    public static int atoi(String s) {
        int i, numMinuses = 0;

        s = s.trim();
        for (i = 0; i < s.length() && (s.charAt(i) == '-' || Character.isDigit(s.charAt(i))); i++)
            if (s.charAt(i) == '-')
                numMinuses++;
        if (i != 0 && numMinuses < 2)
            return Integer.parseInt(s.substring(0, i));

        return 0;
    }
}

Related

  1. getTimestamp(Object o)
  2. getTimestamp(Object value)
  3. getTimestamp(Object value, int columnType)
  4. getTimestamp(ResultSet rs, String colName)
  5. getTimestamp(ResultSet rs, String strColName)
  6. getTimestamp(String dateStr)
  7. getTimestamp(String dateString, String format)
  8. getTimestamp(String sqlDateTime)
  9. getTimestamp(String Str)