Java Date to Timestamp getTimeStamp(Date date, String time)

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

Description

get Time Stamp

License

Open Source License

Parameter

Parameter Description

Return

The date with the time component.

Declaration

public static Date getTimeStamp(Date date, String time) 

Method Source Code

//package com.java2s;
/*/*  www. j av  a  2 s .  com*/
 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
 * Institute of Systems Science, National University of Singapore
 *
 * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved.
 * Use of this source code is subjected to the terms of the applicable license
 * agreement.
 *
 * -----------------------------------------------------------------
 * REVISION HISTORY
 * -----------------------------------------------------------------
 * DATE             AUTHOR          REVISION      DESCRIPTION
 * 10 March 2012    Chen Changfeng   0.1            Class creating
 *                                        
 *                                        
 *                                        
 *                                        
 * 
 */

import java.util.*;
import java.sql.Timestamp;

public class Main {
    private static String timezone = "Asia/Singapore";

    /**
     * Return the Timestamp instance based on the current server date.
     *
     * @return the current server timestamp
     */
    public static Timestamp getTimestamp() {
        return new Timestamp(getDate().getTime());
    }

    public static Timestamp getTimestamp(int year, int month, int day) {
        return new Timestamp(getDate(year, month, day).getTime());
    }

    /**
     * @param date, time
     * @return The date with the time component.
     */
    public static Date getTimeStamp(Date date, String time) {

        if (date == null)
            return null;

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = Integer.parseInt(time.substring(0, 2));
        int minute = Integer.parseInt(time.substring(3));
        cal.set(Calendar.HOUR, hour);
        cal.set(Calendar.MINUTE, minute);
        return cal.getTime();
    }

    /**
     * Returns the date instance based on the current system date
     * <p>
     * @return Current System date.
     */
    public static Date getDate() {
        TimeZone tz = TimeZone.getTimeZone(timezone);
        return Calendar.getInstance(tz).getTime();
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();

        // Clear all fields first
        cal.clear();

        cal.set(year, month - 1, day);

        return cal.getTime();
    }
}

Related

  1. getTimestamp(Date date)
  2. getTimestamp(Date date)
  3. getTimestamp(Date date)
  4. getTimestamp(Date date)
  5. getTimestamp(Date date)
  6. removeTimestamp(Date date)
  7. timestamp(Calendar date)
  8. timestamp(Date d)
  9. timeStamp(Date date)