计时器、日期格式化、支付倒计时

660 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

日期格式化:

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");//如果想要的日期格式是20220520,那就写成yyyyMMdd,如果想要的日期格是是2022-05-20,那就写成yyyy-MM-dd,注意y一定要小写,M一定要大写,d一定要小写,不然会报错哦
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);

支付倒计时:待支付的订单30分钟后自动消失

    //  30-(创建时间-当前时间)
    //当前时间
    long l = System.currentTimeMillis()/1000;
    //2021-07-01 15:32:36
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date parse = format.parse(dataBean.getCreateTime());
        long time = parse.getTime()/1000;
        long rent_time = 30*60 - (l - time);
        mCountDownTimer = countDown(rent_time, holder.goTime, dataBean);
        mCountDownTimer.start();
        Log.d("当前时间create", time+"");
        Log.d("当前时间current", l+"");
        Log.d("当前时间create-current", time-l+"");
        Log.d("当前时间", time-l+"\n"+rent_time+"\n");
    } catch (ParseException pE) {
        pE.printStackTrace();
        Log.d("当前时间failed", pE.getMessage()+"");
    }
}
public CountDownTimer countDown(long time, final TextView pTextView, SelectQuestionOrderListBean.DataBean pBean) {
    CountDownTimer countDownTimer = new CountDownTimer(time * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String time = DateUtil.formatTime(millisUntilFinished);
            String[] split = time.split(":");
            String minute = split[0];
            String second = split[1];
            int i = Integer.parseInt(minute);
            if (i < 1) {
                pTextView.setText(second + "秒" + "后消失");
            } else {
                pTextView.setText(minute + "分" + second + "秒" + "后消失");
            }

        }

        @Override
        public void onFinish() {
            //  pTextView.setText("00:00");
            pTextView.setVisibility(View.GONE);
            cancel();
        }
    };
    return countDownTimer;
}

Chronometer计时器:

布局引用这个控件即可

 ch.setBase(SystemClock.elapsedRealtime());
        ch.setFormat("%S");
        ch.start();
        ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                if(SystemClock.elapsedRealtime()-ch.getBase()>=60000){
                    ch.stop();
                }
            }
        });

实现录音的计时: 当时的需求是录音的计时,时分秒分别展示在不同的框中,实现方法类似于倒计时(倒计时是减减,这个用加加就可以计时了),最后把日期格式化一下,即可,我这里不同数字要在不同框,所以我进行了截取

mMusicStart.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    startRecord();
                    mTimer = new Timer();
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            mContext.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    time++;
                                    String s = FormatMiss(time);
                                    String substring = s.substring(0, 1);
                                    String substring1 = s.substring(1, 2);
                                    String substring2 = s.substring(3, 4);
                                    String substring3 = s.substring(4, 5);
                                    one.setText(substring);
                                    two.setText(substring1);
                                    three.setText(substring2);
                                    four.setText(substring3);
                                    Log.i("凉城时间", substring+"\n"+substring1+"\n"+substring2+"\n"+substring3);
                                }
                            });
                        }
                    }, 0, 1000);
                    Toast.makeText(mContext, "按住录音中", Toast.LENGTH_SHORT).show();
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    stopRecord();
                    mTimer.cancel();
                    Toast.makeText(mContext, "松开结束录音", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

日期格式化:

// 格式化 时:分:秒  自己想要哪部分截取哪部分
  public static String FormatMiss(int miss) {
    String hh = miss / 3600 > 9 ? miss / 3600 + "" : "0" + miss / 3600;
    String mm = (miss % 3600) / 60 > 9 ? (miss % 3600) / 60 + "" : "0" + (miss % 3600) / 60;
    String ss = (miss % 3600) % 60 > 9 ? (miss % 3600) % 60 + "" : "0" + (miss % 3600) % 60;
    return hh + ":" + mm + ":" + ss;
  }