Android 日期时间类

##Android 日期类##

Unix时间戳在计算上最为方便和灵活,效率也高;而Date和Calendar则在一些具体的日期计算上更为便利。
Date和Calendar自动根据手机所设置的时区来调整时间戳的,也就是该时区真实的时间戳
SimpleDateFormat可设置地区和时区,而是获取指定时区的格式化时间
使用TimeZone.getDefault().getRawOffset()方法来得到Calendar和Date的时间差

Date nowDateStr = new Date(); // 默认时区
Calendar calendar = Calendar.getInstance(); //默认时区
Log.d(TAG, "calendar long = " + calendar.getTimeInMillis());
Log.d(TAG, "    date long = " + nowDateStr.getTime());
Log.d(TAG, "calendar date = " + calendar.getTime());
Log.d(TAG, "         date = " + nowDateStr);
	
Calendar在不手动设置时区时,是与系统默认时区相关的。
在手动修改时区后,不能使用calendar.getTime方法来直接获取Date日期,因为此时的日期与setTime时的值相同
想要正确获取修改时区后的时间,应该通过calendar的get方法。

Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("GMT-08:00")); // 指定时区
// 获取的还是默认时区
Log.d(TAG, "calendar1 long = " + calendar1.getTimeInMillis());
Log.d(TAG, "calendar1 date = " + calendar1.getTime());
// 获取的是指定时区
Log.d(TAG, "calendar HOUR_OF_DAY = " + calendar1.get(Calendar.HOUR_OF_DAY));
Log.d(TAG, "calendar MINUTE = " + calendar1.get(Calendar.MINUTE));
Log.d(TAG, "calendar SECOND = " + calendar1.get(Calendar.SECOND));
Log.d(TAG, "calendar MILLISECOND = " + calendar1.get(Calendar.MILLISECOND));

int offset = TimeZone.getDefault().getRawOffset(); // 与标准时间戳(GMT+00:00)差

Calendar类型

1.Calendar 转化 String

//获取当前时间的具体情况,如年,月,日,week,date,分,秒等 
Calendar calendat = Calendar.getInstance(); //默认时区
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());

2.String 转化Calendar

String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

3.Calendar转化Date

Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();

4.Long 转换成时间

long timeLong = 1468307486765L;  //GMT+08:00保存的 Tue Jul 12 2016 15:11:26
//1、手机默认时区转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); 
calendar.setTimeInMillis(timeLong);
String parsDateStr = sdf.format(calendar.getTime());
//2、指定时区转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); // 格式化为指定时区
String parsDateStr = sdf.format(calendar.getTime());

5.Calendar转化Unix时间戳

Calendar calendar = Calendar.getInstance();//获取当前日历对象
long unixTime = calendar.getTimeInMillis();//获取当前时区下日期时间对应的时间戳
long unixTimeGMT = unixTime - TimeZone.getDefault().getRawOffset();//获取标准格林尼治时间下日期时间对应的时间戳

Date类型

1.Date 转化String

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());

2.String 转化Date

String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);

3.Date 转化Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());

4.Date 转化Unix时间戳

Date date = new Date();//获取当前日期对象
unixTimeGMT = unixTime = date.getTimeInMillis();//获取当前时区下日期时间对应的时间戳

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
String dateString = "2010-12-26 03:36:25";//设定具有指定格式的日期字符串
unixTimeGMT = unixTime = format.format(date);//获取当前时区下日期时间对应的时间戳

获取当前Android的年月日时分秒的时间

Android的文件有建议用Time代替Calendar。用Time对CPU的负荷会较小。(取出时间只有24小时模式)

1)

Time time = new Time("GMT+8"); // 或 Time t=new Time(); 
time.setToNow();   
int year = time.year;   
int month = time.month;   
int day = time.monthDay;   
int minute = time.minute;   
int hour = time.hour;   
int sec = time.second;   

2)
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

获取Android系统时间是24小时制还是12小时制

ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
		android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24")){
	Log.i("activity","24");
}

参考:http://www.2cto.com/kf/201207/139551.htmlhttp://fjfj910.iteye.com/blog/1202219

转载于:https://my.oschina.net/u/2501904/blog/700291

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值