java时间格式化函数

时间格式化类位于java.text下

DateFormat和SimpleDateFormat是用来格式化一个日期的,不是用来生成一个日期的 如果要生成一个日期可以用Date类或者Calendar类

DateFormate类

  • 是日期/时间格式化子类的抽象类,它以语言无关的方式格式化和分析日期或时间。

  • DateFormat 为抽象类,不能直接使用,所以需要常用的子类java.text.SimpleDateFormate

public SimpleDateFormat(String pattern)

SimpleDateFormate类

SimpleDateFormat是DateFormat类的子类

SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类,它允许进行日期格式化(日期→文本)、解析(文本→日期)和规范化。

SimpleDateFormat 使得可以选择任何用户定义的日期/时间格式的模式。

SimpleDateFormat 类主要有如下 3 种构造方法。

1、SimpleDateFormat():用默认的格式和默认的语言环境构造 SimpleDateFormat。 
2、SimpleDateFormat(String pattern):用指定的格式和默认的语言环境构造SimpleDateFormat。 
3、SimpleDateFormat(String pattern,Locale locale):用指定的格式和指定的语言环境构造 SimpleDateFormat。

选择不同的pattern输出不同的时间格式(注意格式分隔符)

SimpleDateFormate常用方法

1、将给定的 Date成日期/时间字符串,并将结果附加到给定的 StringBuffer

public StringBuffer format(Date date,StringBuffer toAppendTo,FieldPosition pos)
//date - 要格式化为日期时间字符串的日期时间值。 
//toAppendTo - 要附加新的日期时间文本。 
//pos - 格式化位置。 在输入:对齐字段,如果需要。 输出:对齐字段的偏移量。

2、从字符串中解析文本以产生一个Date

public Date parse(String text,ParsePosition pos)
//text - A String ,其中一部分应解析。 
//pos - 具有 ParsePosition的索引和错误索引信息的 ParsePosition对象。
// 结果 
//一个Date从字符串中解析出来。 万一出错,返回null。 
//异常 
//NullPointerException - 如果 text或 pos为空。 

对于两个函数的练习

  1. 将获取的时间格式化为指定格式字符串

  2. 将字符串格式化为Date类型数据

package com.person;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
    @Test
    public void simpleDateFormateTest(){
        Date date = new Date();
        //使用simpleDateFormate对其进行格式化
        System.out.println("使用parttern1格式化时间:输出年月日");
        String parttern1 ="yyyy-MM-dd";//parttern1
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(parttern1);
        String format = simpleDateFormat1.format(date);
        System.out.println(format);
        System.out.println("使用parttern2格式化时间:输出年-月-日-时-分-秒");
        String parttern2="yyyy-MM-dd HH:mm:ss";//parttern2
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(parttern2);
        String format2 = simpleDateFormat2.format(date);
        System.out.println(format2);
​
        //将指定字符串转化为时间格式
        String date1="1990-2-3";
        String date2="1990-2-3 14:2:2";
        try {
            System.out.println("使用parttern1格式将字符转换为时间格式");
            Date parse = simpleDateFormat1.parse(date1);
            System.out.println("使用parttern2格式将字符转换为时间格式");
            Date parse1 = simpleDateFormat2.parse(date2);
            System.out.println(parse);
            System.out.println(parse1);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
​
​
    }
}
​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值