zoukankan      html  css  js  c++  java
  • Redis在java项目中的使用

    以生成付款编号为例:

        /**
         * 获取付款编号
         * @return
         */
        public static String getPaymentBatchNumber(){
            Long seqCode = RedisUtil.incr(SEQ_PAYMENT_BATCH_NUMBER);//注1,插入到redis里
            String seqString =fourCodeFormat.format(seqCode);//注2,格式化为统一格式
            refreshExpire(SEQ_PAYMENT_BATCH_NUMBER);//注3,设置过期时间
            return getDateString()+seqString;
        }
    public static final String SEQ_PAYMENT_BATCH_NUMBER = "sequence:seqPaymentBatchNumber"; //注1,批量付款批次号
    public static DecimalFormat fourCodeFormat=new DecimalFormat("0000");//注2,格式化为统一格式
    //DecimalFormat我们经常要将数字进行格式化,比如取2位小数,这是最常见的。Java 提供DecimalFormat类,帮你用最快的速度将数字格式化为你需要的样子。下面是一个例子:
      public static void main(String[]args){
        double pi = 3.1415927; //圆周率
        //取一位整数
        System.out.println(new DecimalFormat("0").format(pi));   //3
        //取一位整数和两位小数
        System.out.println(new DecimalFormat("0.00").format(pi)); //3.14
        //取两位整数和三位小数,整数不足部分以0填补。
        System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
        //取所有整数部分
        System.out.println(new DecimalFormat("#").format(pi));   //3
        //以百分比方式计数,并取两位小数
        System.out.println(new DecimalFormat("#.##%").format(pi)); //314.16%
         long c =299792458;  //光速
        //显示为科学计数法,并取五位小数
        System.out.println(new DecimalFormat("#.#####E0").format(c)); //2.99792E8
        //显示为两位整数的科学计数法,并取四位小数
        System.out.println(new DecimalFormat("00.####E0").format(c)); //29.9792E7
        //每三位以逗号进行分隔。
        System.out.println(new DecimalFormat(",###").format(c));   //299,792,458
        //将格式嵌入文本
        System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));
      
      }
    private static void refreshExpire(String seqName){
            Date nowDate = new Date();
            Date date = DateUtils.truncate(nowDate, Calendar.DAY_OF_MONTH);
            date = DateUtils.addDays(date, +1);
            RedisUtil.expireAt(seqName, date);
        }
    //注3,设置过期时间
  • 相关阅读:
    小公司的技术分享怎么搞
    当他们说「独立思考」时,到底在说什么
    java使用tika批量识别文件的真实mime类型
    hibernate:Not all named parameters have been
    mybatis出错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.yyy.dao.ProjectMapper.getById
    Mysql Hibernate报错
    tomcat中多个端口部署项目
    Windows Server 2012多个winlogon.exe LogonUI.exe dwm.exe ChsIME.exe进程
    springboot使用profile指定不同配置(尚硅谷)
    springboot配置文件占位符(尚硅谷)
  • 原文地址:https://www.cnblogs.com/zhuxiang1029/p/15619379.html
Copyright © 2011-2022 走看看