安卓动态时间获取

作者: 螢火蟲的瘋狂 | 来源:发表于2017-11-16 11:51 被阅读0次

    完全拷贝的,仅供参考。代码来源

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/white">

    android:id="@+id/mytime"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    android:textColor="@android:color/black"

    android:textSize="36sp"/>

    下面就是Java代码

    import android.app.Activity;

    import android.os.Bundle;

    import android.os.Handler;

    import android.os.Message;

    import android.text.format.DateFormat;

    import android.widget.TextView;

    public class TestActivity extends Activity {

    private static final intmsgKey1=1;

    private TextView mTime;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.time);

    mTime= (TextView) findViewById(R.id.mytime);

    new TimeThread().start();

    }

    public class TimeThread extends Thread {

    @Override

    public void run () {

    do {

    try {

    Thread.sleep(1000);

    Messagemsg=newMessage();

    msg.what=msgKey1;

    mHandler.sendMessage(msg);

    }

    catch (InterruptedException e) {

    e.printStackTrace();

    }

    } while(true);

    }

    }

    private HandlermHandler=newHandler() {

    @Override

    public void handleMessage (Message msg) {

    super.handleMessage(msg);

    switch (msg.what) {

    case msgKey1:

    mTime.setText(getTime());

    break;

    default:

    break;

    }

    }

    };

    //获得当前年月日时分秒星期

    public String getTime(){

    final Calendarc=Calendar.getInstance();

    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

    StringmYear=String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份

    StringmMonth=String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份

    StringmDay=String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码

    StringmWay=String.valueOf(c.get(Calendar.DAY_OF_WEEK));

    StringmHour=String.valueOf(c.get(Calendar.HOUR_OF_DAY));//时

    StringmMinute=String.valueOf(c.get(Calendar.MINUTE));//分

    StringmSecond=String.valueOf(c.get(Calendar.SECOND));//秒

    if("1".equals(mWay)){

    mWay="天";

    }else if("2".equals(mWay)){

    mWay="一";

    }else if("3".equals(mWay)){

    mWay="二";

    }else if("4".equals(mWay)){

    mWay="三";

    }else if("5".equals(mWay)){

    mWay="四";

    }else if("6".equals(mWay)){

    mWay="五";

    }else if("7".equals(mWay)){

    mWay="六";

    }

    return mYear + "年" + mMonth + "月" + mDay+"日"+"  "+"星期"+mWay+"  "+mHour+":"+mMinute+":"+mSecond;

    }

    }

    相关文章

      网友评论

        本文标题:安卓动态时间获取

        本文链接:https://www.haomeiwen.com/subject/zjllvxtx.html