Java 的时间出来类

导言:在java程序中很多时候需要使用时间处理函数,这里就简单的罗列下时间处理的java类;

一、SimpleDataFormat

public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。

把一种时间格式转化为另一种时间格式:

        String str = "2016-09-28 10:01:10";
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try{
            //最后的转化格式日期:yyyy-MM-dd HH:mm
            System.out.println(format2.format(format1.parse(str)));抽象类DateFormat以及它的子类实体类SimpleDateFormat。这两个类都位于java.text包中,是专门用于日期格式化和解析的类。而这两项工作的核心就是我们为此设定的Pattern,我们可以称之为“日期格式表达式”。

            //Wed Sep 28 10:01:10 CST 2016
            Date da = format1.parse(str);
            System.out.println(da);
        } catch (ParseException e){
            e.printStackTrace();
        }

输出结果:

2016-09-28 10:01
Wed Sep 28 10:01:10 CST 2016
日期格式化与解析

中文解释含义:
G:年代标识,表示是公元前还是公元后
y:年份
M:月份
d:日
h:小时,从1到12,分上下午
H:小时,从0到23
m:分钟
s:秒
S:毫秒
E:一周中的第几天,对应星期几,第一天为星期日,于此类推
z:时区
D:一年中的第几天
F:这一天所对应的星期几在该月中是第几次出现
w:一年中的第几个星期
W:一个月中的第几个星期
a:上午/下午标识
k:小时,从1到24
K:小时,从0到11,区分上下午

    public static void simpleDateTest(Date time){
        SimpleDateFormat fmat1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        SimpleDateFormat fmat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat fmat3 = new SimpleDateFormat("yy/MM/dd HH:mm");
        SimpleDateFormat fmat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat fmat5 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
        SimpleDateFormat fmat6 = new SimpleDateFormat("一年中的第 D 天 一年中第 w 个星期 一个月中第 W 个星期 在一天中 k时 z时区");

        System.out.println(fmat1.format(time));
        System.out.println(fmat2.format(time));
        System.out.println(fmat3.format(time));
        System.out.println(fmat4.format(time));
        System.out.println(fmat5.format(time));
        System.out.println(fmat6.format(time));

        System.out.println();
        System.out.println("toString: " + time.toString());
        System.out.println("toGMTString: " + time.toGMTString());
        System.out.println("toLocaleString: " + time.toLocaleString());

        // 创建一个日期格式表达式
        String pattern = "年代:G;年份:y;月份:M;日:d;时(1~12):h;时(0~23):H;分:m;秒:s;毫秒:S;星期:E;上/下午:a;时区:z";
        //使用日期格式创建SimpleDateFormat对象
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        System.out.println("1位: " + df.format(time));
    }
结果:

2016-09-28 10:01
Wed Sep 28 10:01:10 CST 2016
2016年09月29日 12时08分11秒
2016-09-29 12:08
16/09/29 12:08
2016-09-29 12:08:11
2016年09月29日 12时08分11秒 星期四
一年中的第 273 天 一年中第40个星期 一个月中第5个星期 在一天中12时 CST时区

toString: Thu Sep 29 12:08:11 CST 2016
toGMTString: 29 Sep 2016 04:08:11 GMT
toLocaleString: 2016-9-29 12:08:11

二、Calendar类

Calendar是一个抽象类,不可以通过new来获取一个实例,可以通过类方法getinstance()获取此类型的一个通用的对象;

1.使用实类化

Calendar cal = Calendar.getInstance();

Calendar lendar=Calendar.getInstance();

int year=2015;

int month=12;

int day=21;

calendar.set(year,month,day);
2.去当天日期

  Calendar calendar = Calendar.getInstance();  

int cur_month=calendat.get(Calendar.MONTH)+1;

int cur_year=calendar.get(Calendar.YEAR);

int cur_day=calendar.get(Calendar.DAY_OF_MONTH);

3.after函数跟before函数的用法当需要知道某一天的日期是否比今日的日期早或者晚之类时;可以计算出2个时间段之间的日期;
思路如下:我们利用set函数设定一个开始时间,一个结束时间,然后使用before或者after函数

public static List<Data> findDates(Date  dBegin,Data dEnd)

{
  List lDate=new ArrayList();
  lDate.add(dBegin);

  Calendar calBegin=Calendar.getinstance();
  calBegin.setTime(dBegin);

  Calendar calEnd=Calendar.getinstance();
  calEnd.setTime(dEnd);

  while(dEnd.after(caBegin.getTime()))

{

    calBegin.add(Calendar.DAY_OF_MONTH,1);//这里有问题待解决,Calendar的实例可以有add 函数?干啥用的?暂时不

    lDate.add(calBegin.getTime());
}
return lDate;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值