美文网首页
Android 小技巧之日期/时间/星期/上午

Android 小技巧之日期/时间/星期/上午

作者: Kevin_小飞象 | 来源:发表于2021-03-18 11:56 被阅读0次
每日一图.jpg

1. 效果图 12.jpg

2. strings.xml

    <string name="week_sunday">星期日</string>
    <string name="week_monday">星期一</string>
    <string name="week_tuesday">星期二</string>
    <string name="week_wednesday">星期三</string>
    <string name="week_thursday">星期四</string>
    <string name="week_friday">星期五</string>
    <string name="week_saturday">星期六</string>

    <string name="day_am">上午</string>
    <string name="day_pm">下午</string>>

3. DateUtils.java

/**
 * Created on 2021/3/18 11:22
 *
 * @author Gong Youqiang
 */
public class DateUtils {
    /**
     * 获取时间
     * @param context
     * @return
     */
    public static String getNowTime(Context context){
        Date now = Calendar.getInstance().getTime();
        SimpleDateFormat format;
        if(DateFormat.is24HourFormat(context)){
            format = new SimpleDateFormat("HH:mm", Locale.getDefault());
        } else {
            format = new SimpleDateFormat("hh:mm", Locale.getDefault());
        }
        return format.format(now);
    }


    /**
     * 获取日期
     * @return
     */
    public static String getNowDate(){
        Date now = Calendar.getInstance().getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
        return format.format(now);
    }

    public static String getNowWeek() {
        Date now = Calendar.getInstance().getTime();
        SimpleDateFormat format = new SimpleDateFormat("EEE", Locale.getDefault());
        return format.format(now);
    }

    /**
     * 获取星期几
     * @param context
     * @return
     */
    public static String getNowWeek(Context context) {
        Calendar cal = Calendar.getInstance();
        Date now = Calendar.getInstance().getTime();
        cal.setTime(now);
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return context.getString(R.string.week_sunday);
            case 2:
                return context.getString(R.string.week_monday);
            case 3:
                return context.getString(R.string.week_tuesday);
            case 4:
                return context.getString(R.string.week_wednesday);
            case 5:
                return context.getString(R.string.week_thursday);
            case 6:
                return context.getString(R.string.week_friday);
            case 7:
                return context.getString(R.string.week_saturday);
            default:
                return "";
        }
    }

    /**
     * 获取上下午
     * @param context
     * @return
     */
    public static String getNowAmPm(Context context) {
        Calendar cal = Calendar.getInstance();
        Date now = Calendar.getInstance().getTime();
        cal.setTime(now);
        int i = cal.get(Calendar.AM_PM);
        switch (i) {
            case 0:
                return context.getString(R.string.day_am);
            case 1:
                return context.getString(R.string.day_pm);
            default:
                return "";
        }
    }
}

4.DateManager.java

/**
 * Created on 2021/3/18 11:22
 *
 * @author Gong Youqiang
 */
public class DateManager {
    private String mDate;

    private String mWeek;

    private String mTime;

    private String mAmPm;

    private TimeChangedListener mTimeChangedListener;

    private Timer mTimer;

    private Context mContext;

    public DateManager(Context context, TimeChangedListener listener){
        mTimeChangedListener = listener;
        mContext = context;
    }

    public String getDate() {
        return mDate;
    }

    public String getWeek() {
        return mWeek;
    }
    public String getampm(){
        return mAmPm;
    }

    public String getTime() {
        return mTime;
    }

    public void start() {
        stop();

        mDate = DateUtils.getNowDate();
        mWeek = DateUtils.getNowWeek(mContext);
        mTime = DateUtils.getNowTime(mContext);
        mAmPm = DateUtils.getNowAmPm(mContext);

        if(mTimeChangedListener != null){
            mTimeChangedListener.onChanged(DateManager.this);
        }

        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                String date = DateUtils.getNowDate();
                String week = DateUtils.getNowWeek(mContext);
                String time = DateUtils.getNowTime(mContext);
                String ampm = DateUtils.getNowAmPm(mContext);

                if(!date.equals(mDate) || !week.equals(mWeek) || !time.equals(mTime)|| !ampm.equals(mAmPm)){
                    mDate = date;
                    mTime = time;
                    mWeek = week;
                    mAmPm = ampm;
                    if(mTimeChangedListener != null){
                        mTimeChangedListener.onChanged(DateManager.this);
                    }
                }
            }
        },0,500);
    }

    public void stop() {
        if(mTimer != null){
            mTimer.cancel();
            mTimer = null;
        }
    }

    public interface TimeChangedListener{
        void onChanged(DateManager dateManager);
    }
}

5. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/black"
    android:gravity="center"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_time_day"
        android:layout_width="@dimen/x382"
        android:layout_height="@dimen/y130"
        android:includeFontPadding="false"
        android:text="10:46"
        android:textColor="@android:color/white"
        android:textSize="@dimen/x114"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <LinearLayout
        android:layout_width="@dimen/x338"
        android:layout_height="@dimen/y28"
        android:layout_marginRight="@dimen/x22"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:id="@+id/tv_time_date"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:includeFontPadding="false"
            android:text="2019.01.29"
            android:textColor="@android:color/white"
            android:textSize="@dimen/x28" />

        <TextView
            android:id="@+id/tv_time_week"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/x10"
            android:includeFontPadding="false"
            android:text="星期二"
            android:textColor="@android:color/white"
            android:textSize="@dimen/x28" />

        <TextView
            android:id="@+id/tv_time_AmPm"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/x10"
            android:includeFontPadding="false"
            android:text="下午"
            android:textColor="@android:color/white"
            android:textSize="@dimen/x28" />

    </LinearLayout>

</LinearLayout>

6. MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_time_date)
    TextView mTimeDate;
    @BindView(R.id.tv_time_day)
    TextView mDay;
    @BindView(R.id.tv_time_week)
    TextView mWeek;
    @BindView(R.id.tv_time_AmPm)
    TextView mAmPm;

    private DateManager mDateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mDateManager = new DateManager(this, new DateManager.TimeChangedListener() {
            @Override
            public void onChanged(DateManager dateManager) {
                mTimeDate.post(new Runnable() {
                    @Override
                    public void run() {
                        mTimeDate.setText(dateManager.getDate());
                        mDay.setText(dateManager.getTime());
                        mWeek.setText(dateManager.getWeek());
                        mAmPm.setText(dateManager.getampm());
                    }
                });
            }
        });
        mDateManager.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDateManager!=null) {
            mDateManager.stop();
        }
    }
}

相关文章

网友评论

      本文标题:Android 小技巧之日期/时间/星期/上午

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