PHP's strtotime() in Java

后端 未结 5 1241
终归单人心
终归单人心 2020-11-28 09:42

strtotime() in PHP can do the following transformations:

Inputs:

strtotime(’2004-02-12T15:19:21+00:00′);
strtotime(’Thu, 21 Dec 2000 16:01:07 +0200′);
str         


        
5条回答
  •  猫巷女王i
    2020-11-28 09:58

    Use a Calendar and format the result with SimpleDateFormat:

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

        Calendar now = Calendar.getInstance();
        Calendar working;
        SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    
        working = (Calendar) now.clone();
    
        //strtotime("-2 years")
        working.add(Calendar.DAY_OF_YEAR, - (365 * 2));
        System.out.println("  Two years ago it was: " + formatter.format(working.getTime()));
    
        working = (Calendar) now.clone();
    
        //strtotime("+5 days");
        working.add(Calendar.DAY_OF_YEAR, + 5);
        System.out.println("  In five days it will be: " + formatter.format(working.getTime()));
    

    Fine, it's significantly more verbose than PHP's strtotime(), but at the end of the day, it's the functionality you're after.

提交回复
热议问题