免费建设钓鱼网站平台,网站建设合同技术开发合同范本,网站建设问题,怎么制作网站内容格式化日期表示将日期/时间格式转换为预先定义的日期/时间格式。例如将日期“Fri May 18 15:46:24 CST2016” 格式转换为 “2016-5-18 15:46:24 星期五”的格式。
在 Java 中#xff0c;可以使用 DateFormat 类和 SimpleDateFormat 类来格式化日期。
DateFormat 类
DateFor…格式化日期表示将日期/时间格式转换为预先定义的日期/时间格式。例如将日期“Fri May 18 15:46:24 CST2016” 格式转换为 “2016-5-18 15:46:24 星期五”的格式。
在 Java 中可以使用 DateFormat 类和 SimpleDateFormat 类来格式化日期。
DateFormat 类
DateFormat 是日期/时间格式化子类的抽象类它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化子类如 SimpleDateFormat允许进行格式化也就是日期→文本、解析文本→日期和标准化日期。
在创建 DateFormat 对象时不能使用 new 关键字而应该使用 DateFormat 类中的静态方法 getDateInstance()示例代码如下
DateFormat df DateFormat.getDatelnstance();在创建了一个 DateFormat 对象后可以调用该对象中的方法来对日期/时间进行格式化。DateFormat 类中常用方法。 格式化样式主要通过 DateFormat 常量设置。将不同的常量传入合适的方法中以控制结果的长度。
DateFormat 类的常量如下。 SHORT完全为数字如 12.5.10 或 5:30pm。MEDIUM较长如 May 102016。LONG更长如 May 122016 或 11:15:32am。FULL是完全指定如 Tuesday、May 10、2012 AD 或 11:l5:42am CST。使用 DateFormat 类格式化曰期/时间的示例如下
// 获取不同格式化风格和中国环境的日期
DateFormat df1 DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
DateFormat df2 DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
DateFormat df3 DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
DateFormat df4 DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);// 获取不同格式化风格和中国环境的时间
DateFormat df5 DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINA);
DateFormat df6 DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA);
DateFormat df7 DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);
DateFormat df8 DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINA);// 将不同格式化风格的日期格式化为日期字符串
String date1 df1.format(new Date());
String date2 df2.format(new Date());
String date3 df3.format(new Date());
String date4 df4.format(new Date());// 将不同格式化风格的时间格式化为时间字符串
String time1 df5.format(new Date());
String time2 df6.format(new Date());
String time3 df7.format(new Date());
String time4 df8.format(new Date());// 输出日期
System.out.println(SHORT date1 time1);
System.out.println(FULL date2 time2);
System.out.println(MEDIUM date3 time3);
System.out.println(LONG date4 time4);运行该段代码输出的结果如下
SHORT19-10-15 上午9:30
FULL2019年10月15日 星期一 上午09时30分43秒 CST
MEDIUM2019-10-15 9:30:43
LONG2019年10月15日 上午09时30分43秒DateFormat 类中方法与常量结合使用通过使用 DateFomat 类可以对日期进行不同风格的格式化。
SimpleDateFormat 类
如果使用 DateFormat 类格式化日期/时间并不能满足要求那么就需要使用 DateFormat 类的子类——SimpleDateFormat。
SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类它允许进行格式化日期→文本、解析文本→日期和规范化。SimpleDateFormat 使得可以选择任何用户定义的日期/时间格式的模式。
SimpleDateFormat 类主要有如下 3 种构造方法。 SimpleDateFormat()用默认的格式和默认的语言环境构造 SimpleDateFormat。SimpleDateFormat(String pattern)用指定的格式和默认的语言环境构造 SimpleDateF ormat。SimpleDateFormat(String pattern,Locale locale)用指定的格式和指定的语言环境构造 SimpleDateF ormat。SimpleDateFormat 自定义格式中常用的字母及含义。
使用 SimpleDateFormat 类格式化当前日期并打印日期格式为“xxxx 年 xx 月 xx 日星期 xxx 点 xx 分 xx 秒”代码如下
import java.text.SimpleDateFormat;
import java.util.Date;public class Test {public static void main(String[] args) {Date now new Date(); // 创建一个Date对象获取当前时间// 指定格式化格式SimpleDateFormat f new SimpleDateFormat(今天是 yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒);System.out.println(f.format(now)); // 将当前时间袼式化为指定的格式}
}该程序的运行结果如下
今天是 2019 年 10 月 15 日 星期一 09 点 26 分 23 秒