Java Timestamp Create toTimestampByHour(Date date, int hour)

Here you can find the source of toTimestampByHour(Date date, int hour)

Description

to Timestamp By Hour

License

Open Source License

Declaration

public static Timestamp toTimestampByHour(Date date, int hour) 

Method Source Code

//package com.java2s;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static Timestamp toTimestampByHour(Date date, int hour) {
        String h = Integer.toString(hour);
        if (h.length() == 1)
            h = "0" + h;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        String endDate = now + " " + h + ":00:00";

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timpestamp = null;
        try {/*from  w w  w.  j  a va  2s .c om*/
            timpestamp = new Timestamp(sdf2.parse(endDate).getTime());

        } catch (ParseException e) {

            e.printStackTrace();
        }

        return timpestamp;
    }
}

Related

  1. toTimestamp(String value)
  2. toTimestamp(String yyyymmddhhmmss)
  3. toTimestamp2(long seconds, int fraction, int width)
  4. toTimestamp2(long seconds, int nanos)
  5. toTimestamp2(long value, int nanos, int meta)
  6. toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss)
  7. toTimestampFromLong(long millis)
  8. toTimestampFromTime(String time)