欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP与Java对比学习日期时间函数

程序员文章站 2024-02-23 09:38:40
废话少说先来看php中 date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49 使用函数date(),输出当前是月份中的第几天,参数...

废话少说先来看php中

date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49

使用函数date(),输出当前是月份中的第几天,参数:string类型 d

例如:echo date("d"); 输出 13

使用函数date(),输出当前是星期中的第几天,参数:string类型 d或者 n

例如:

echo date("d"); 输出 fri

echo date("n"); 输出 5

echo date("l"); 输出 friday

使用函数date(),输出当前月份中的第几月,参数:string类型 n

echo date("n"); 输出 5

使用函数date(),判断当前年份是否是闰年,参数:string类型 l

echo date("l"); 输出 1

strtotime():把字符串类型日期格式转成时间戳

使用函数strtotime(),打印前一天日期,参数:string类型 “-1 day”

echo date("y-m-d h:i:s",strtotime("-1day"));输出 2016-05-12 15:27:33

使用函数strtotime(),打印明天日期,参数:string类型 “+1 day”

echo date("y-m-d h:i:s",strtotime("+1 day"));输出 2016-05-14 15:28:29

使用函数strtotime(),打印下周日期,参数:string类型 “+1 week”

echo date("y-m-d h:i:s",strtotime("+1 week"));;输出 2016-05-20 15:29:35

使用函数strtotime(),打印下一个月日期,参数:string类型 “+1 month”

echo date("y-m-d h:i:s",strtotime("+1 month")); 输出:2016-06-13 15:37:42

使用函数strtotime(),打印下周一日期,参数:string类型 “last mondy”

echo date("y-m-d h:i:s",strtotime("next monday")); 输出:2016-05-16 00:00:00

使用函数strtotime(),打印下周零两天两小时两秒后日期,参数:string类型组合一下

echo date("y-m-d h:i:s",strtotime("+1 week 2 day 2 hour")); 输出 2016-05-22 17:34:34

==================================================================

java版:

java.util.date类

获取date对象,new出来

调用date对象的gettime()方法,获取时间戳(毫秒值)

java.text.simpledateformat类

获取simpledateformat对象,new出来,构造参数:"yyyy-mm-dd hh:mm:ss"

调用simpledateformat对象的format()方法,获取string类型的日期,参数:date对象

例如:

          date date=new date();
          simpledateformat format=new simpledateformat("yyyy-mm-dd hh:mm:ss");
          system.out.println(format.format(date));