android会走的时间,android中获取时间

1)通过calendar类获取

Calendar calendar = Calendar.getInstance();

int moth = calendar.get(Calendar.MONTH);

int date = calendar.get(Calendar.DATE);

int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;

2)未知方法

获取当前时间:System.currentTimeMillis()    获得的是从1970年1月1日到当前时间的总秒数

获取时区:TimeZone.getDefault();

举例说明:

例1:

Calendar now = Calendar.getInstance();

TimeZone timeZone = now.getTimeZone();

long totalMilliseconds = System.currentTimeMillis() + timeZone.getRawOffset();

long totalSeconds = totalMilliseconds / 1000;

int currentSecond = (int)(totalSeconds % 60);

long totalMinutes = totalSeconds / 60;

int currentMinute = (int)(totalMinutes % 60);

long totalHours = totalMinutes / 60;

int currentHour = (int)(totalHours % 24);

int totalDays = (int)(totalHours / 24);

int goDays = 0;

int surplusDays = 0;

int goYears = 0;

int leapyear = 0;

for(int i = 1970;goDays

{

if(i%400==0||(i%4==0&&i%100!=0))

{goDays = goDays+366; leapyear = 1;}

else {goDays = goDays +365; leapyear = 0;}

goYears++;

}

String output = "Current time is " + currentHour + ":"

+ currentMinute + ":" + currentSecond + " GMT"+(1970+goYears-1);

JOptionPane.showMessageDialog(null, output);

例2:

long l = System.currentTimeMillis();

Date date = new Date(l);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

System.out.println(cal.get(Calendar.YEAR));

System.out.println(cal.get(Calendar.MONTH) + 1);// 月份计算是从0作为1开始的。

System.out.println(cal.get(Calendar.DATE));

System.out.println(cal.get(Calendar.HOUR));

System.out.println(cal.get(Calendar.MINUTE));

System.out.println(cal.get(Calendar.SECOND));

3)通过java包获得时间

public void getTime(){

time = (TextView) findViewById(R.id.test_1);

java.util.Date date = new java.util.Date();

time.setText(date.toString());

}

加背景颜色的可以改成toLocalString()或者toGMTString()

三者的区别(格式)是:(1),toSting():Tue Sep 20 07:56:00 GMT+00:00 2011

(2),toLocaleString():Sep 20,2011 08:00:00 AM  (这种格式比较适合大众)

(3),toGMTString():20 Sep 2011 08:00:00 GMT

4),最简单的,通过android自带的空间完成时钟的添加

分别是AnalogClock机械时钟和DigitalClock数字时钟,只需要在android的xml布局文件中添加两个空间即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值