Getting the next hour in java

核能气质少年 提交于 2019-12-11 03:35:35

问题


Please provide the code getting next hour from database. For example I am getting a timestamp from the database as:

    String sqlupdate = "select UploadedDate from CAF_Entry where Status='new' and Reference=0   order by UploadedDate limit 1";
    System.out.println("sql query"+sqlupdate);
    Statement stupdate = con.createStatement();
    ResultSet rsupdate = stupdate.executeQuery(sqlupdate);
    if(rsupdate.next())
    {
      date22=rsupdate.getTimestamp("UploadedDate");
      String stringdate=date22.toString();`

I am getting the date 2012-11-12 10:30:00

How do I get the next hour, such as 2012-11-12 11:00:00?


回答1:


Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date22.getTime);
calendar.add(Calendar.HOUR, 1);

After that you can use SimpleDateFormat to format the date to a String if you want it in that form. Calendar.getTime() will return a java.util.Date object if you need one.




回答2:


String sqlupdate = "select UploadedDate from CAF_Entry where Status='new' and Reference=0   order by UploadedDate limit 1";
System.out.println("sql query"+sqlupdate);
Statement stupdate = con.createStatement();
ResultSet rsupdate = stupdate.executeQuery(sqlupdate);
if(rsupdate.next())
{
  DateTime date22=new DateTime(rsupdate.getTimestamp("UploadedDate").getTime()).plusHours(1);

  String stringdate=date22.toString();`

You'll need to use joda-time, which you should be using as the JDK Date/Calendar clusterfsck is horrid.




回答3:


Use the following code snippet

Calendar c = Calendar.getInstance();
c.setTimeInMillis(date22.getTime());
c.add(Calendar.HOUR, 1);
Date d = c.getTime();


来源:https://stackoverflow.com/questions/13495289/getting-the-next-hour-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!