简体   繁体   English

将PHP语句覆盖到Java中

[英]Coverting PHP statement into Java

I had to convert the following PHP script into a JSP scriplet: 我必须将以下PHP脚本转换为JSP脚本:

<?php
 $cache_expire = 60*60*24*365;
 header("Pragma: public");
 header("Cache-Control: max-age=".$cache_expire);
 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?>

I was able to do: 我能够做到:

<%! long cacheExpire = 60*60*24*365; %>

<% 
 response.setHeader("Pragma", "public");
 response.setHeader("Cache-Control", "max-age=" + cacheExpire);
 response.setHeader("Expires", "....?..."); // what do I put in the second string ?
%>

But I am unable to convert the last statement in the PHP script to JSP. 但是我无法将PHP脚本中的最后一条语句转换为JSP。 I just understood this in Java: 我只是在Java中了解这一点:

new GregorianCalendar().getTime() + cacheExpire

but this is incorrect to implement. 但这是不正确的实现。

How do I convert the last statement of PHP to a Java one? 如何将PHP的最后一个语句转换为Java?

This is the exact equivalent of your PHP code above: 这与上面的PHP代码完全等效:

<%! long cacheExpire = 60*60*24*365; %>

<% 
 response.setHeader("Pragma", "public");
 response.setHeader("Cache-Control", "max-age=" + cacheExpire);

 DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
 df.setTimeZone(TimeZone.getTimeZone("GMT"));
 long expire = (long) System.currentTimeMillis() + (cacheExpire * 1000);
 String expires = df.format(new Date(expire)) + " GMT";

 response.setHeader("Expires", expires);
%>

tricky part was convert between different date format tokens of java and php and get the java date on GMT timezone, hope you find it useful :-) 棘手的部分是在Java和php的不同日期格式标记之间进行转换,并在GMT时区获得Java日期,希望您发现它有用:-)

尝试这个:

response.setDateHeader("Expires", System.currentTimeMillis() + cacheExpire);

You have to use Calender.get(Calendar.YEAR) etc. to achieve this. 您必须使用Calender.get(Calendar.YEAR)等来实现此目的。 There is no way of expressing date format in Java the way you can do it in PHP, however, you can get field values just the same. 用Java不能像用PHP那样表达日期格式,但是,可以得到相同的字段值。

You should use SimpleDateFormat class to format the Date object in Java. 您应该使用SimpleDateFormat类在Java中格式化Date对象。 For example, check the following code, replace stringDatePattern by valid date format like "yyyy-MM-dd" . 例如,检查以下代码,将stringDatePattern替换为有效的日期格式,例如"yyyy-MM-dd"

new SimpleDateFormat(stringDatePattern).format(new Date()) + cacheExpire

In the above snippet, new Date() returns the current time, which is formatted using SimpleDateFormat as per the pattern passed. 在上面的代码段中, new Date()返回当前时间,该时间根据传递的模式使用SimpleDateFormat格式化。 Then you just concatenate the cacheExpire variable. 然后,您只需串联cacheExpire变量。

That example does exactly what you want: 该示例正是您想要的:

int cacheExpire = 60 * 60 * 24 * 365;

Calendar cal = Calendar.getInstance();
cal.add( Calendar.SECOND, cacheExpire );
SimpleDateFormat format = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss 'GMT'" );
format.setTimeZone( TimeZone.getTimeZone( "GMT" ) );

response.setHeader( "Expires", format.format( cal.getTime() ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM