java 日期小于早上七点半_Java中的日期与时间

@(java 中的日期与时间)

日期与时间

最常用的几个类,date、dateformat、calendar、locale

date

1.无参构造方法

//根据当前系统默认的毫秒值创建时间对象

public date() {

this(system.currenttimemillis());

}

2.根据毫秒值创建时间对象

long time = 1000*60*60;

date d = new date(time);

3.传入年月日时分秒创建时间对象

date d2 = new date(20,10,10,11,11,11)

//这得到的时间并不是20-10-10这种

//下面是源码

public date(int year, int month, int date, int hrs, int min, int sec) {

int y = year + 1900;

// month is 0-based. so we have to normalize month to support long.max_value.

if (month >= 12) {

y += month / 12;

month %= 12;

} else if (month < 0) {

y += calendarutils.floordivide(month, 12);

month = calendarutils.mod(month, 12);

}

basecalendar cal = getcalendarsystem(y);

cdate = (basecalendar.date) cal.newcalendardate(timezone.getdefaultref());

cdate.setnormalizeddate(y, month + 1, date).settimeofday(hrs, min, sec, 0);

gettimeimpl();

cdate = null;

}

dateformat

dateformat是日期时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期和时间。

他是抽象类,使用其子类simpledateformat

dateformat 类本身的内部提供了可以直接为其实例化的操作

//得到日期的dateformat对象:

public static final dateformat getdateinstance();

//得到日期时间的dateformat对象:

public static final dateformat getdatetimeinstance();

//使用dateformat类格式化date类日期

public final string format(date date)

simpledateformat类

simpledateformat函数的继承关系:

java.lang.object

|

+—-java.text.format

|

+—-java.text.dateformat

|

+—-java.text.simpledateformat

//构造方法:

public simpledateformat(string pattern)

//转换:

public date parse(string source)throws parseexception //-->此时取得的是全部时间数。

//格式化:

public final string format(date date) //-->将时间重新格式化成字符串显示。

把date转化成指定的日期格式

public class formatdatetime {

public static void main(string[] args) {

simpledateformat myfmt=new simpledateformat("yyyy年mm月dd日 hh时mm分ss秒");

simpledateformat myfmt1=new simpledateformat("yy/mm/dd hh:mm");

simpledateformat myfmt2=new simpledateformat("yyyy-mm-dd hh:mm:ss");//等价于now.tolocalestring()

simpledateformat myfmt3=new simpledateformat("yyyy年mm月dd日 hh时mm分ss秒 e ");

simpledateformat myfmt4=new simpledateformat("一年中的第 d 天 一年中第w个星期 一月中第w个星期 在一天中k时 z时区");

date now=new date();

system.out.println(myfmt.format(now));

system.out.println(myfmt1.format(now));

system.out.println(myfmt2.format(now));

system.out.println(myfmt3.format(now));

system.out.println(myfmt4.format(now));

system.out.println(now.togmtstring());

system.out.println(now.tolocalestring());

system.out.println(now.tostring());

}

}

把给定的字符串中的日期提取为date

这样做,通常是一个日期字符串,但不是想要的格式,可以先转化为date,再转化为其它格式。

import java.text.* ;

import java.util.* ;

public class datedemo05{

public static void main(string args[]){

string strdate = "2008-10-19 10:11:30.345" ;

// 准备第一个模板,从字符串中提取出日期数字

string pat1 = "yyyy-mm-dd hh:mm:ss.sss" ;

// 准备第二个模板,将提取后的日期数字变为指定的格式

string pat2 = "yyyy年mm月dd日 hh时mm分ss秒sss毫秒" ;

simpledateformat sdf1 = new simpledateformat(pat1) ; // 实例化模板对象

simpledateformat sdf2 = new simpledateformat(pat2) ; // 实例化模板对象

date d = null ;

try{

d = sdf1.parse(strdate) ; // 将给定的字符串中的日期提取出来

}catch(exception e){ // 如果提供的字符串格式有错误,则进行异常处理

e.printstacktrace() ; // 打印异常信息

}

system.out.println(sdf2.format(d)) ; // 将日期变为新的格式

}

};

dateformat 和simpledateformat 的区别

1.dateformat 可以直接使用,但其本身是一个抽象类,可以根据locate指定的区域得到对应的日期时间格式

2.simpledateformat 类是dateformat 类的子类,一般情况下来讲 dateformat 类很少会直接使用。而都使用simpledateformat 类完成。

1、dateformat:是抽象类,所以使用子类simpledateformat

2、simpledateformat类构造方法:

public simpledateformat():使用默认模式。

public simpledateformat(string pattern):使用给定的模式。

api规定的模式:y m d h m s

3、simpledateformat类成员方法:

public final string format(date date):日期格式化为日期字符串。

pattern date parse(string source):日期字符串解析为日期。

calendar

calendar:它为特定瞬间与一组诸如 year、month、day_of_month、hour 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

一、构造方法

protected calendar() :由于修饰符是protected,所以无法直接创建该对象。需要通过别的途径生成该对象。

二、成员方法

calendar类的成员方法

static calendar getinstance()

使用默认时区和区域设置获取日历。通过该方法生成calendar对象。如下所示:

calendar cr=calendar.getinstance();

public void set(int year,int month,int date,int hourofday,int minute,int second) 设置日历的年、月、日、时、分、秒。

public int get(int field) 返回给定日历字段的值。所谓字段就是年、月、日等等。

public void settime(date date) 使用给定的date设置此日历的时间。date------calendar

public date gettime() 返回一个date表示此日历的时间。calendar-----date

abstract void add(int field,int amount) 按照日历的规则,给指定字段添加或减少时间量。

public long gettimeinmillies() 以毫秒为单位返回该日历的时间值。

三、日历字段

日历字段包含以下两种:一种是表示时间的单位,例如年、月、日等等。另一种是具体的日期,例如一月、二月、三月、一日、二日、三日、一点钟、两点钟等等具体的时间。前一种一般时获取的时候使用,后一种一般判断的时候使用。

时间单位字段:

year 年 minute 分

day_of_week_in_month

某月中第几周

month 月 second/millisecond 秒/毫秒 week_of_month 日历式的第几周

date 日 day_of_month

和date一样

day_of_year 一年的第多少天

hour_of_day 时 day_of_week 周几 week_of_year 一年的第多少周

am_pm 返回1则表示是下午,返回0表示上午。

public class calendardemo {

public static void main(string[] args) {

// 其日历字段已由当前日期和时间初始化:

calendar rightnow = calendar.getinstance(); // 子类对象

// 获取年

int year = rightnow.get(calendar.year);

// 获取月

int month = rightnow.get(calendar.month);

// 获取日

int date = rightnow.get(calendar.date);

//获取几点

int hour=rightnow.get(calendar.hour_of_day);

//获取上午下午

int moa=rightnow.get(calendar.am_pm);

if(moa==1)

system.out.println("下午");

else

system.out.println("上午");

system.out.println(year + "年" + (month + 1) + "月" + date + "日"+hour+"时");

rightnow.add(calendar.year,5);

rightnow.add(calendar.date, -10);

int year1 = rightnow.get(calendar.year);

int date1 = rightnow.get(calendar.date);

system.out.println(year1 + "年" + (month + 1) + "月" + date1 + "日"+hour+"时");

}

}

注意:month是从0开始的,而月份是从1开始的,所以month需要加一

locale

locale一般用于国际化,locale表示一个特定的地区,locale类支持非常多的国家和地区。我们可以通过以下方法,查看locale支持的全部区域:

locale[] ls = locale.getavailablelocales();

for (locale locale:ls) {

system.out.println("locale :"+locale);

}

主要构造函数

// locale的构造函数

locale(string language)

locale(string language, string country)

locale(string language, string country, string variant)

使用例子:

e5a6f1d10085e992d4c78e988621d81f.png

有关java国际化,另外一篇文章有描述。

希望与广大网友互动??

点此进行留言吧!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值