2019年1月

马上又要过年了,不知道小伙伴们准备好过年了吗。也不知道从哪一年开始了,手机红包越来越多了,给新年平添了不少色彩。支付宝从前年开始推出看一款新的活动——集福。集福的方式也是多种多样了,付的种类也是越来越多了。小编在这里分享下自己扫福的小技巧。
今年支付宝有新增了沾沾卡,据说有人占到了花花卡,看来还不错呢,沾沾卡呢可以通过每天的在线支付获得一张,也可以扫福得到哦,下面的福字就可以哦,大家可以试试哦。
微信图片_20190131144313.jpg

小编的五福已经合成了,哈哈哈哈,缺少敬业福的小伙伴们加点紧了,下面小编给大家几个,出来敬业福比较多的福字,大家可以试着扫一扫。多扫几次,可能会跳转到其他页面哦。(下面的图片有点大,加载的有点忙,大家稍等一下哈)

微信图片_20190131144508.png

好了,小编这里给大家贡献了10个福字,也祝愿大家十全十美吧。小编要喂小鸡糖葫芦去喽。

小编已经集齐2套喽!!!
QQ图片20190203153916.png

时间转换为时间戳(10位)

    public static long dateToTimestamp(String time) {
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 try {
     Date date = simpleDateFormat.parse(time);
     long ts = date.getTime()/1000;
     return ts;
 } catch (ParseException e) {
     return 0;
 }
    }

时间戳转换诶时间

    public static String timestampToDate(long time) {
 if (time < 10000000000L) {
     time = time * 1000;
 }
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
 return sd;
    }

获取当前时间戳(13位)

    public static long nowData() {
// 方法 一
// long data = System.currentTimeMillis();
// 方法 二
long data = Calendar.getInstance().getTimeInMillis();
// 方法三
// long data = new Date().getTime();
return data;

    }

获取当前时间(yyyy-MM-dd hh:mm:ss)

    public static String nowTime() {
 Date currentTime = new Date();// 获取当前时间
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 格式化时间
 String dateString = formatter.format(currentTime);// 转换为字符串
 return dateString;
    }

比较时间大小

     public static int compare_date(String DATE1, String DATE2) {
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
   try {
       Date dt1 = df.parse(DATE1);
       Date dt2 = df.parse(DATE2);
       if (dt1.getTime() > dt2.getTime()) {
           return 1;
       } else if (dt1.getTime() < dt2.getTime()) {
           return -1;
       } else {
           return 0;
       }
   } catch (Exception exception) {
       exception.printStackTrace();
   }
   return 0;
        }

字符串转换为时间

    public static Date strToData(String data,String format) {
 Date date = null;
 try {
     date = new SimpleDateFormat(format).parse(data);
 } catch (ParseException e) {
     e.printStackTrace();
 }
 return date;
    }

时间格式化

    public static String parseTimestamp(String format) {
Date now = new Date();
SimpleDateFormat f = new SimpleDateFormat(format);
return f.format(now);
    }

附录1:为什么会生成13位的时间戳,13位的时间戳和10时间戳分别是怎么来的
原来java的date默认精度是毫秒,也就是说生成的时间戳就是13位的,而像c++或者php生成的时间戳默认就是10位的,因为其精度是秒。
附录2:13位时间戳如何转换成10位时间戳
本来以为java中有设置可以修改其时间精度,后来在百度上没有找到,就只能采用其它方法来转化,这里提供两种方式来转换。
第一种:通过substring方法,将13位的时间戳最后三位数字截取。
第二种:将13位时间戳除以1000取整。
小编使用的是第二种,在上面的代码中也有提现。

int转换为String(int i=100)

第一种方法:s=i+""; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象。

String转换为int(Sting s="100")

第一种方法:i=Integer.parseInt(s); //直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象,

字符串转换为float

float f = Float.parseFloat(str);

字符串转double

String str = "123.002";Double d ;d = Double.parseDouble(str);

String转换为byte[]

String string = "hello world";
byte[] bytes = string.getBytes();

byte[]转换为String

String s = new String(bytes);

通过Base64将String与byte进行转换

import java.util.Base64;
public class test 
{
    public static void main(String[] args) 
    {
        byte[] bytes = "hello world".getBytes();
        String encoded = Base64.getEncoder().encodeToString(bytes);
        byte[] decoded = Base64.getDecoder().decode(encoded);
        for(byte a : decoded)
            System.out.println(a);
        System.out.println( new String(decoded) );
    }
}

String转换为数组

String str = "a,b,bb,dd";

  1. String[] strArr = str.split(",");
  2. char[] charArr = str.toCharArray();
  3. byte[] byteArr = str.getBytes();

字符数组转换为字符串:

char[] c1 = {'a','b','c'};String str = new String(c1);

需求:首先获取一个base64格式的图片,然后保存在指定文件夹内,并且新建时间文件夹进行区分,如果时间文件夹存在就直接存储,时间文件夹不存在就新建文件夹。

 /**
     * 保存图片
     *
     * @param base64image 图片base64字符串
     * @param name 图片保存之后的名称
     * @return 文件名
     */
    public static String saveBase64Image(String base64image,String name) {

        Date date = new Date();
        String dataForm = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String filePath = PropertiesTools.applicationProperty("app.image.path") + dataForm + '/';
        File file = new File(filePath);
        if (!file.exists()) {//如果文件夹不存在
            file.mkdir();//创建文件夹
        }

        String fileType = FileTools.base64imageType(base64image);// "a.png";
        String fileName = FileTools.randomFileName(null);
        String fileContent = FileTools.base64imageContent(base64image);
        String imageFullName = name + "." + fileType;

        FileTools.saveBase64Images(fileContent, filePath + imageFullName);
        return dataForm+'/'+imageFullName;
    }

    /**
     * 保存图片
     *
     * @param base64str 图片base64字符串
     * @param filePath  完整的文件路径(包含文件名)
     * @return 文件地址
     */
    public static String saveBase64Images(String base64str, String filePath) {

        // 参数校验
        if (base64str == null || filePath == null)
            return null;

        // 检查目录是否存在,同时生成目录
        String fileDirectory = FileTools.directoryFromPath(filePath);
        if (!FileTools.generateDirectory(fileDirectory)) {
            return null;
        }

        // 存储文件
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(base64str);
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }

            //生成jpeg图片
            OutputStream out = new FileOutputStream(filePath);
            out.write(b);
            out.flush();
            out.close();

            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

在项目的实际开发过程中会经常用到有关时间的处理,比如获取当前的时间,获取当前的时间戳等。而且我们为了更加的规范往往还需要对时间的格式进行统一的处理。并且还有灵活的对事件以及时间戳进行转换。在实际的项目开发过程中,往往把这些内容抽取处理,作为单独的工具类,小编这里就自己整理了一个时间处理的工具类,若果有不完善的地方,大家一起讨论。

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

public class DateTools {

    /**
     * 当前时间-时间戳
     *
     * @return 根路径
     */
    public static int timestamp() {
        long time = System.currentTimeMillis();
        return (int) (time / 1000);
    }

    /**
     * 获取当前时间-时间(yyyy-MM-dd HH:mm:ss)
     *
     * @return
     */
    public static String timesNow() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 格式化时间,默认“yyyy-MM-dd hh:mm:ss”
     *
     * @param timestamp
     * @return
     */
    public static String parseTimestamp(long timestamp) {
        return parseTimestamp(timestamp, "yyyy-MM-dd hh:mm:ss");
    }

    /**
     * 当前格式化时间
     *
     * @return
     */
    public static String formatNow(String aFmt) {
        Date date = new Date();
        String fmt = aFmt;
        if (fmt == null) {
            fmt = "yyyy-MM-dd hh:mm:ss";
        }
        SimpleDateFormat dateFmt = new SimpleDateFormat(fmt);
        String dateStr = dateFmt.format(date);
        return dateStr;
    }

    /**
     * 格式化时间
     *
     * @param timestamp
     * @param fmt       时间格式化字符串
     * @return
     */
    public static String parseTimestamp(long timestamp, String fmt) {
        Date date = new Date(timestamp * 1000);
        SimpleDateFormat dateFmt = new SimpleDateFormat(fmt);
        String dateStr = dateFmt.format(date);
        return dateStr;
    }

    /**
     * 时间转换为时间戳
     *
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 时间戳(10位)转换为时间
     * @param time
     * @return
     */
    public static String timestampToDate(Long time) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time*1000))));
        return sd;
    }

}