Java 时间格式

基础时间格式转换

常用的日期格式化转换符
常用的日期格式化转换符

具体应用方法,如下示例:

    Date date=new Date();
    String year=String.format("%tY",date);
    String month=String.format("%tB",date);
    String day=String.format("%td",date);
    String date2=String.format("%tc",date);
    System.out.println("完整日期是:"+date2);
    System.out.println("今年是"+year+"年");
    System.out.println("现在是"+month);
    System.out.println("今天是"+day+"号");

运行结果

常用时间格式化转换符
常用时间格式化转换符

具体应用如下示例:

    Date date=new Date();
    String hour=String.format("%tH",date);
    String minute=String.format("%tM",date);
    String second=String.format("%tS",date);
    String time=String.format("%tQ",date);
    System.out.println("现在是"+hour+"时"+minute+"分"+second+"秒");
    System.out.println("1970-01-01 00:00:00 至现在经过"+time+"毫秒");

常见的日期和时间组合的格式化
常见的日期和时间组合的格式化
具体应用如下:

   Date date=new Date(); 
   String time=String.format("%tc",date);
   String form=String.format("%tF",date);
    System.out.println("全部的时间信息是:"+time);
    System.out.println("年-月-日格式:"+form);

或者喜欢简单粗暴

Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
System.out.println(dateString); 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值