您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java获取当前系统时间的前12小时时间

小志的博客 发布时间:2019-10-18 10:33:27 ,浏览量:1

1、代码如下:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test1 {
	public static void main(String[] args){
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		DateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 //(1)获取当前时间
		LocalDateTime date = LocalDateTime.now();
		String nowtime=dateTimeFormatter.format(date);
		System.out.println("系统当前时间      :"+nowtime);
		//(2)获取当前时间的前12小时时间
		LocalDateTime localDateTime = date.minusHours(12);
		String nowtime12=dateTimeFormatter.format(localDateTime);
		System.out.println("获取当前时间的前12小时时间      :"+nowtime12);
	}
}
2、效果如下:
系统当前时间      :2019-10-18 10:31:14
获取当前时间的前12小时时间      :2019-10-17 22:31:14
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0387s