java_时间与日期的格式化

今天的学习过程中碰到一些小问题,,时间与日期处理起来特别麻烦,,于是有了这篇博文;
简单的整理了一下如何打印出规范的时间格式;

import java.time.Year;
import java.util.Date;

public class time {
    public static void main(String[] args) {
        Date date = new Date();
        //进行格式化
        String str = String.format("%tF",date);
        System.out.println(date);
        System.out.println(str);
        
        String year = String.format("%tY", date);
        String month = String.format("%tB", date);
        String day = String.format("%td", date);

        System.out.println("今年是"+year+"年");
        System.out.println("现在是"+month);
        System.out.println("今天是第"+day+"天");

        String hour = String.format("%tH", date);
        String minute = String.format("%tM", date);
        String second = String.format("%tS", date);

        System.out.println("现在是"+hour+"时"+minute+"分"+second+"秒");

        String str1 = String.format("%tF",date);
        System.out.println("tF格式"+str1);
        String str2 = String.format("%tD",date);
        System.out.println("tD格式"+str2);
        String str3 = String.format("%tc",date);
        System.out.println("tc格式"+str3);
        String str4 = String.format("%tR",date);
        System.out.println("tR格式"+str4);
    }
}

输出结果为:

Thu Aug 08 18:39:47 CST 2019
2019-08-08
今年是2019年
现在是八月
今天是第08天
现在是18时39分47秒
tF格式2019-08-08
tD格式08/08/19
tc格式星期四 八月 08 18:39:47 CST 2019
tR格式18:39

关于时间与日期的格式多种多样,可以参考java基本类库API文档;
API的下载教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值