simpledateformat格式(如何在Java中使用SimpleDateFormat对日期格式进行转换)

发布时间:2025-12-11 02:27:00 浏览次数:1

Java的特点有哪些

Java的特点有哪些1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

SimpleDateFormat是处理日期格式转换的类。

官方API_1.8关于SimpleDateFormat继承于DateFormate截图:

SimpleDateFormat的构造器如下:

SimpleDateFormat中的格式定义,常用的用红色框圈出:

中文解释:

y : 年
M : 年中的月份
D : 年中的天数
d : 月中的天数
w : 年中的周数
W : 月中的周数
a : 上下/下午
H : 一天中的小时数(0-23)
h : 一天中的小时数(0-12)
m : 小时中的分钟
s : 分钟中的秒数
S : 毫秒数

SimpleDateFormat方法:

继承于DateFormate的方法:

SimpleDateFormat常用方法和常用格式定义使用实例:

packagecom.lanhuigu.java.format;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassFormatTest{publicstaticvoidmain(String[]args)throwsParseException{//**************1.(format,parse)最常用方法实例*************System.out.println("----------最常用方法---------");//格式SimpleDateFormatsdf1=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//时间Datedate1=newDate();System.out.println("操作前的时间:"+date1);//日期类型时间-》转换为定义格式-》字符串类型时间/**注意:format(Datedate)这个方法来自于SimpleDateFormat的父类DateFormat*/Stringstr1=sdf1.format(date1);System.out.println("字符串类型时间:"+str1);//字符串类型时间-》转换为定义格式-》日期类型时间DatedateF1=sdf1.parse(str1);System.out.println("日期类型时间:"+dateF1);//**************2.关于常用格式分析*************System.out.println("----------常用格式分析---------");/**y:年*M:年中的月份*D:年中的天数*d:月中的天数*w:年中的周数*W:月中的周数*a:上下/下午*H:一天中的小时数(0-23)*h:一天中的小时数(0-12)*m:小时中的分钟*s:分钟钟的秒数*S:毫秒数*///注意,为了省事,这个地方把常用的都放进来了,一起打印看效果,//在实际使用中,根据需求进行相应格式转换SimpleDateFormatsdf2=newSimpleDateFormat("yyyy-MM-dd,w,W,a,HH:mm:ss,SS");Stringstr2=sdf2.format(newDate());System.out.println("日期类型时间:"+str2);System.out.println("字符串类型时间:"+sdf2.parse(str2));//**************2.关于构造器使用技巧分析*************System.out.println("----------构造器使用技巧分析---------");/**构造器:*SimpleDateFormat();*SimpleDateFormat(Stringpattern);*SimpleDateFormat(Stringpattern,DateFormatSymbolsformatSymbols);*SimpleDateFormat(Stringpattern,Localelocale)*///通过对应构造器构造对象,直接调用方法,简洁写法System.out.println(newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(newDate()));}}
simpledateformat格式
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477