介绍
这篇文章主要介绍了如何在Java中使用SimpleDateFormat对日期格式进行转换,小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随小编来看看吧!
Java的特点有哪些
Java的特点有哪些 1. java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2. java具有简单性,面向对象,分布式、安全性,平台独立与可移植性,动态性等特点。 3。使用Java可以编写桌面应用程序,网络应用程序,分布式系统和嵌入式系统应用程序等。
SimpleDateFormat是处理日期格式转换的类。
<强>官方API_1.8关于SimpleDateFormat继承于DateFormate截图:强>
<强> SimpleDateFormat的构造器如下:强>
<强> SimpleDateFormat中的格式定义,常用的用红色框圈出:强>
<强>中文解释:强>
y:年
M:年中的月份
D:年中的天数
D:月中的天数
w:年中的周数
w:月中的周数
:上/下下午
H:一天中的小时数(0-23)
H:一天中的小时数(经历)
M:小时中的分钟
s:分钟中的秒数
s:毫秒数
<强> SimpleDateFormat方法:强>
<强>继承于DateFormate的方法:强>
<强> SimpleDateFormat常用方法和常用格式定义使用实例:强>
package com.lanhuigu.java.format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class  FormatTest { public static  void main (String [], args), throws ParseException {//,* * * * * * * * * * * * * * 1。(格式,解析)最常用方法实例* * * * * * * * * * * * *, System.out.println(“- - - - - - - - - - -最常用方法- - - - - - -产生绯闻;);//,格式, SimpleDateFormat sdf1 =, new SimpleDateFormat (“yyyy-MM-dd  HH: mm: ss");//,时间, Date date1 =, new 日期(); System.out.println(“操作前的时间:“,+,date1);//,日期类型时间——》转换为定义格式——》字符串类型时间,/*, ,,,,*,注意:,格式(Date 日期)这个方法来自于SimpleDateFormat的父类DateFormat ,,,*/String str1 =, sdf1.format (date1); System.out.println(“字符串类型时间:“,+,str1);//,字符串类型时间——》转换为定义格式——》日期类型时间, Date dateF1 =, sdf1.parse (str1); System.out.println(“日期类型时间:“,+,dateF1);//,* * * * * * * * * * * * * * 2。关于常用格式分析* * * * * * * * * * * * *, System.out.println(“- - - - - - - - - - -常用格式分析- - - - - - -产生绯闻;);/*, ,,,,*,y :,年, ,,,,*,M :,年中的月份, ,,,,*,D :,年中的天数, ,,,,*,d :,月中的天数, ,,,,*,w :,年中的周数, ,,,,*,W :,月中的周数, ,,,,*,a :,上下/下午, ,,,,*,H :,一天中的小时数(0-23), ,,,,*,h :,一天中的小时数(经历), ,,,,*,m :,小时中的分钟, ,,,,*,s :,分钟钟的秒数, ,,,,*,S :,毫秒数, ,,,*///,注意,为了省的事,这个地方把常用的都放进来了,一起打印看效果,,//,在实际使用中,根据需求进行相应格式转换, SimpleDateFormat sdf2 =, new SimpleDateFormat (“yyyy-MM-dd w w, HH: mm: ss, SS"); String str2 =, sdf2.format (new 日期()); System.out.println(“日期类型时间:“,+,str2); System.out.println(“字符串类型时间:“,+,sdf2.parse (str2));//,* * * * * * * * * * * * * * 2。关于构造器使用技巧分析* * * * * * * * * * * * *, System.out.println(“- - - - - - - - - - -构造器使用技巧分析- - - - - - -产生绯闻;);/*, ,,,,*,构造器:, ,,,,*,SimpleDateFormat (),, ,,,,*,SimpleDateFormat (String 模式),, ,,,,*,SimpleDateFormat (String 模式,DateFormatSymbols formatSymbols),, ,,,,*,SimpleDateFormat (String 模式,Locale 地区), ,,,*///,通过对应构造器构造对象,直接调用方法,简洁写法, System.out.println (new SimpleDateFormat (“yyyy-MM-dd  HH: mm: ss") .format (new 日期())); } }如何在Java中使用SimpleDateFormat对日期格式进行转换