当前位置: 移动技术网 > IT编程>开发语言>Java > Java获得当前时间前指定几个小时具体时间的方法示例

Java获得当前时间前指定几个小时具体时间的方法示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java获得当前时间前指定几个小时具体时间的方法。分享给大家供大家参考,具体如下:

package getbeforehourdemo;
import java.text.simpledateformat;
import java.util.calendar;
//获取当前时间,指定前面多少小时的时间 
//返回格式yyyymmddhhmmss 
public class getbeforehourtime {
  public static string getbeforebyhourtime(int ihour){ 
     string returnstr = ""; 
     calendar calendar = calendar.getinstance(); 
     calendar.set(calendar.hour_of_day, calendar.get(calendar.hour_of_day) - ihour); 
     simpledateformat df = new simpledateformat("yyyymmddhhmmss"); 
     returnstr = df.format(calendar.gettime()); 
     return returnstr; 
  } 
  public static void main(string[] args) {
    string beforetime=getbeforebyhourtime(2);
    system.out.println(beforetime); 
  }
}

运行结果如下图所示:

ps:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:

在线万年历日历:

在线阴历/阳历转换工具:

unix时间戳(timestamp)转换工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网