您正在查看: yhbin 发布的文章

Java 时间格式转换

final Date date = new Date();
System.out.println("date:"+date);
final Timestamp timestamp = new Timestamp(date.getTime());
System.out.println("timestamp:"+timestamp);      System.out.println("date.getTime():"+date.getTime());
final Calendar calendar =Calendar.getInstance();
System.out.println("calendar:"+calendar);
final Instant instant = Instant.now();
System.out.println("instant:"+instant);
final LocalDateTime localDateTime = LocalDateTime.now();    System.out.println("localDateTime:"+localDateTime);
final ZonedDateTime zonedDateTime = ZonedDateTime.now();   System.out.println("zonedDateTime:"+zonedDateTime);

输出:
date:Wed Dec 01 10:37:24 CST 2021
timestamp:2021-12-01 10:37:24.501
date.getTime():1638326244501
calendar:java.util.GregorianCalendar[time=1638326244506,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=335,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=37,SECOND=24,MILLISECOND=506,ZONE_OFFSET=28800000,DST_OFFSET=0]
instant:2021-12-01T02:37:24.508Z
localDateTime:2021-12-01T10:37:24.569
zonedDateTime:2021-12-01T10:37:24.570+08:00[Asia/Shanghai]

Date 转 字符串

Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTime = df.format(date);
输出:
2021-12-01 10:27:10

时间戳 转 字符串

Date date = new Date();
long time = date.getTime();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sf.format(new Date(time)));
输出:时间戳->Date->字符串
2021-12-01 10:52:38

字符串 转 时间戳

String date_str = "2021-12-01 10:52:38";
try {
   SimpleDateFormat sdf = new    SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   System.out.println(sdf.parse(date_str).getTime()/1000);
} catch (Exception e) {
   e.printStackTrace();
}

输出:字符串->Date->时间戳
1638327158

转 Date

//Timestamp转Date
Date timestampDate = new Date(timestamp.getTime());
//Calendar转Date
Date calendarDate = calendar.getTime();
//Instant转Date
Date instantDate = Date.from(instant);
//LocalDateTime转Date
Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//ZonedDateTime转Date
Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());

转 Instant

//Date转Instant
Instant dateInstant = date.toInstant();
//Timestamp转Instant
Instant timestampInstant = timestamp.toInstant();
//Calendar转Instant
Instant calendarInstant = calendar.toInstant();
//LocalDateTime转Instant
Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
//ZonedDateTime转Instant
Instant zonedDateTimeInstant = zonedDateTime.toInstant();

转 LocalDateTime

//Date转LocalDateTime
LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转LocalDateTime
LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
//Calendar转LocalDateTime
LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
//Instant转LocalDateTime
LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//ZonedDateTime转LocalDateTime
LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();

转 ZonedDateTime

//Date转ZonedDateTime
ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转ZonedDateTime
ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
//Calendar转ZonedDateTime
ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
//Instant转ZonedDateTime
ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
//LocalDateTime转ZonedDateTime
ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

转 Timestamp

//Date转Timestamp
Timestamp dateTimestamp = new Timestamp(date.getTime());
//Calendar转Timestamp
Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
//Instant转Timestamp
Timestamp instantTimestamp = Timestamp.from(instant);
//LocalDateTime转Timestamp
Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
//ZonedDateTime转Timestamp
Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());

转 Calendar

//Date转Calendar
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(date);
//Timestamp转Calendar
Calendar timestampCalendar = Calendar.getInstance();    timestampCalendar.setTimeInMillis(timestamp.getTime());
//Instant转Calendar
Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
//LocalDateTime转Calendar
Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
//ZonedDateTime转Calendar
Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);

PHP将URL地址参数进行加密传输提高网站安全性

function keyED($txt,$encrypt_key){       
    $encrypt_key =    md5($encrypt_key);
    $ctr=0;       
    $tmp = "";       
    for($i=0;$i<strlen($txt);$i++)       
    {           
        if ($ctr==strlen($encrypt_key))
        $ctr=0;           
        $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
        $ctr++;       
    }       
    return $tmp;   
}    
function encrypt($txt,$key)   {
    $encrypt_key = md5(mt_rand(0,100));
    $ctr=0;       
    $tmp = "";      
     for ($i=0;$i<strlen($txt);$i++)       
     {
        if ($ctr==strlen($encrypt_key))
            $ctr=0;           
        $tmp.=substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
        $ctr++;       
     }       
     return keyED($tmp,$key);
} 
    
function decrypt($txt,$key){       
    $txt = keyED($txt,$key);       
    $tmp = "";       
    for($i=0;$i<strlen($txt);$i++)       
    {           
        $md5 = substr($txt,$i,1);
        $i++;           
        $tmp.= (substr($txt,$i,1) ^ $md5);       
    }       
    return $tmp;
}
function encrypt_url($url,$key){
    return rawurlencode(base64_encode(encrypt($url,$key)));
}
function decrypt_url($url,$key){
    return decrypt(base64_decode(rawurldecode($url)),$key);
}
function geturl($str,$key){
    $str = decrypt_url($str,$key);
    $url_array = explode('&',$str);
    if (is_array($url_array))
    {
        foreach ($url_array as $var)
        {
            $var_array = explode("=",$var);
            $vars[$var_array[0]]=$var_array[1];
        }
    }
    return $vars;
}
 
$key_url_md_5 = 'mdaima.com-123-scc'; //可以更换为其它的加密标记,可以自由发挥

WorkerMan推送数据到前端

1.服务器开放端口号
2.宝塔开放端口号
3.workerman文件夹放到相应目录下
4.php start.php start

命名空间

为避免相同分类名,相同方法名冲突,引入命名空间概念

若是在框架中,则引用auto_load文件就可引入,此处没有用框架,则需要引入,include 为物理引入,use为逻辑引入,只有先物理引入才能用use。

现在我们来看使用:
1.include引入类文件
2.use引入
3.new类
4.调用类中方法

注意

类的namespace可自己随意写,此处写了haibin\src\Lib所以文件实例化时就写成
use haibin\src\Lib\aaa; new aaa();
如果写haibin\Lib,则需写成 use haibin\Lib\aaa; new aaa();
总之一句话,就是类文件命名空间是什么,文件实例化时就写什么+类名,必须写明use哪个类名,不然不知道要用哪个类

二维数组根据某一列排序

//根据字段bad_num对数组$bad进行降序排列
        $last_names = array_column( $bad, 'bad_num' );
        array_multisort( $last_names, SORT_DESC, $bad );

a标签点击图片链接下载到本地

$url='http://apply.com/static/upload/head/20190711/d612d32ac92be41a64ffb091fad01508.png';
$file_name = time().'.png';    //保存的图片名称
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);   
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($url));
ob_clean();
flush();
readfile($url);
exit;

crontab调用svn更新版本库

*/1 * * * * /usr/bin/svn update /home/wwwroot/test.huazhenginfo.com

# U:表示从服务器收到文件更新了 G:表示本地文件以及服务器文件都已更新,而且成功的合并了  其他的如下: A:表示有文件或者目录添加到工作目录 R:表示文件或者目录被替换了. C:表示文件的本地修改和服务器修改发生冲突       本文转自建波李 51CTO博客,原文链接:http://blog.

TP5隐藏index.php

一,找到/public/.htaccess文件,如果你的入口文件已经移动到根目录下,那么你的.htaccess文件也要剪切到根目录下,总之要确保.htaccess跟入口的index.php保持同级。

1.Apache:
<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
2.Nginx(在Nginx.conf中添加):
location / { // …..省略部分代码
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        break;
    }
}
3.phpstudy:
<IfModule mod_rewrite.c> 
Options +FollowSymlinks -Multiviews 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] 
</IfModule>

/dev/null 2>&1 用法

1. >/dev/null

这条命令的作用是将标准输出1重定向到/dev/null中。 /dev/null代表linux的空设备文件,所有往这个文件里面写入的内容都会丢失,俗称“黑洞”。那么执行了>/dev/null之后,标准输出就会不再存在,没有任何地方能够找到输出的内容。

2. 2>&1

这条命令用到了重定向绑定,采用&可以将两个输出绑定在一起。这条命令的作用是错误输出将和标准输出同用一个文件描述符,说人话就是错误输出将会和标准输出输出到同一个地方。

默认是标准输出:

如果添加输出到文件,则不会在屏幕显示,会写进文件中:

报错时候会显示在屏幕上:

如果则没有 2>&1 则还是会显示在屏幕上,因为只是指定标准输出到文件里,没有指定错误输出到文件里:

添加上之后就会将错误信息写进文件里:

Linux错误排查工具 iftop

[root@localhost ~]# yum install libpcap libpcap-devel ncurses ncurses-devel
[root@localhost ~]# yum install fl ex byacc
[root@localhost ~]# wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
[root@localhost ~]# rpm -ivh epel-release-6-8.noarch.rpm
[root@localhost ~]# yum install iftop