安卓代码素材积累

  1. 广播服务
   //发送广播
                Intent it=new Intent();
                it.setAction("GETNAME");
                it.putExtra("name", account.getText().toString());
                LoginDialog.this.getContext().sendBroadcast(it);
//接收广播
private BroadcastReceiver b=new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("GETNAME")){
                String getName=intent.getStringExtra("name");
                txtView1.setText(getName);
            }

        }

    };

//注册广播
        IntentFilter itf=new IntentFilter();
        itf.addAction("GETNAME");
        this.registerReceiver(b, itf);

2. 使用正则表达式去除字符串中的首尾中括号

String b = a.replaceAll("\\[([^\\]]*)\\]", "$1");

3. 比较两个“11:15“”小时:分钟“形式的时间的大小

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class TimeTest {
      private static final String formatStr = "HH:mm";
       private static SimpleDateFormat sdf=new SimpleDateFormat(formatStr);



            public static void main(String args[]) throws ParseException {
                String tS = "12:50";
                String tE = "13:10";
                if(isInZone(getLong(tS),getLong(tE),getCurrentTime())){
                    System.out.println("ts小于当前时间,tE 大于当前时间!");
                }
                if(TimeCompare(getLong(tS),getLong(tE))){
                    System.out.println("ts 小于 te");
                }

            }


            private static boolean isInZone(long tStart,long tEnd,long t) throws ParseException {
                return tStart <= t && t <= tEnd;
            }

            private static long getLong(String timeStr) throws ParseException {
                return sdf.parse(timeStr).getTime();
            }

            private static long getCurrentTime() throws ParseException {
                return getLong(sdf.format(new Date()));
            }
            private static boolean TimeCompare(long t1,long t2) throws ParseException {
                return t1 <= t2;
            }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值