java中怎么把日期格式化

互联网 19-11-20

java把日期格式化:

import java.text.SimpleDateFormat; import java.util.Date;  public class test{      public static void main(String args[]) {         Date newTime = new Date();         //设置时间格式         SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E");         SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E");         SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E");         SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E");                  //获取的时间,是本机的时间         String formatDate1 = sdf1.format(newTime);         String formatDate2 = sdf2.format(newTime);         String formatDate3 = sdf3.format(newTime);         String formatDate4 = sdf4.format(newTime);          System.out.println(formatDate1);           System.out.println(formatDate2);          System.out.println(formatDate3);          System.out.println(formatDate4);      } }

输出结果如下:

java中使用SimpleDateFormat类的构造函数SimpleDateFormat(String str)构造格式化日期的格式,

通过format(Date date)方法将指定的日期对象格式化为指定格式的字符串.

更多java知识请关注java基础教程。

以上就是java中怎么把日期格式化的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: java
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:java的错误代码怎么看

相关资讯