[英]Difference between SimpleDateFormat and strtotime?
What is the difference between this in PHP 这在PHP中有什么区别
echo strtotime("2013-08-30");
and this in Java 而这在Java中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2013-08-30");
System.out.println(date.getTime());
Shouldn't this give me the same number in the end? 最后不应该给我相同的号码吗?
EDIT 编辑
For example in this case the PHP gives: 1377838800 例如,在这种情况下,PHP提供:1377838800
And Java gives: 1377838800000 Java给出了:1377838800000
Its like PHP loses a few zeroes somewhere along the way 就像PHP在此过程中的某些地方失去了一些零
PHP strtotime() PHP strtotime()
Unix timestamp: Unix时间戳:
This 这个
echo strtotime("2013-08-30");
will return 1377835200
将返回
1377835200
Java Date.getTime() Java Date.getTime()
This 这个
System.out.println(date.getTime());
will return 1377831600000
. 将返回
1377831600000
。
Which means you should perform 这意味着你应该执行
System.out.println(date.getTime() / 1000);
Also, in case of discrepancies, check your timezones. 另外,如果有差异,请检查您的时区。
No. As documented, strtotime
returns the seconds since the Unix epoch; 否。如前所述,
strtotime
返回自Unix时代以来的秒数 ; Date.getTime
returns the milliseconds since the Unix epoch. Date.getTime
返回自Unix时代以来的毫秒数 。
Additionally, you may notice differences in the default time zones used by Java and PHP. 此外,您可能会注意到Java和PHP使用的默认时区有所不同。 (You can set the time zone of a
SimpleDateFormat
if you want to use a specific one.) (如果要使用特定的时区,则可以设置
SimpleDateFormat
的时区。)
I am not familiar with Java, but the strtotime()
function in PHP is exceptionally adept at understanding a date written in pretty much any format that makes sense and returns a date object. 我不熟悉Java,但是PHP中的
strtotime()
函数非常善于理解以几乎任何有意义的格式写的日期并返回日期对象的日期。 I would assume that Java is much more stringent about what inputs you can enter to get the desired result. 我认为Java对于可以输入以获得期望结果的输入要严格得多。
From the PHP docs ( http://php.net/manual/en/function.strtotime.php ) on strtotime: 从strtotime上的PHP文档( http://php.net/manual/en/function.strtotime.php ):
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
All these are much more will return a valid date. 所有这些都将返回有效日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.