12.1Android如何获取当前时间戳?(二合一) | Java Debug 笔记

212 阅读1分钟

本文正在参加「Java主题月 - Java Debug笔记活动」,详情查看<活动链接>

我想要这样的当前时间戳:1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts =  tsTemp.toString();

回答:

可以使用以下代码,我在这个代码上成功跑通:

/**
 * 
 * @return yyyy-MM-dd HH:mm:ss formate date as string
 */
public static String getCurrentTimeStamp(){
    try {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateTime = dateFormat.format(new Date()); // Find todays date

        return currentDateTime;
    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}

回答2:

这是一个易于理解的时间戳,可以起个文件名直接使用,以防万一有人需要与我相同的东西:

package com.example.xyz;

import android.text.format.Time;

/**
 * Clock utility.
 */
public class Clock {

    /**
     * Get current time in human-readable form.
     * @return current time as a string.
     */
    public static String getNow() {
        Time now = new Time();
        now.setToNow();
        String sTime = now.format("%Y_%m_%d %T");
        return sTime;
    }
    /**
     * Get current time in human-readable form without spaces and special characters.
     * The returned value may be used to compose a file name.
     * @return current time as a string.
     */
    public static String getTimeStamp() {
        Time now = new Time();
        now.setToNow();
        String sTime = now.format("%Y_%m_%d_%H_%M_%S");
        return sTime;
    }

}

提问:Spring如何使用多个@RequestMapping注释?

是否可以@RequestMapping在一个方法上使用多个注释?

比如

@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
  return "welcome";
}

回答1:

@RequestMapping有一个String[]值参数,因此您应该能够指定多个值,如下所示:

@RequestMapping(value={"", "/", "welcome"})

关于这个代码,。存在一些争议,有人在使“”或“ /”值在我的应用程序中实际工作时遇到了麻烦。

回答2:

根据我的测试,@RequestMapping(value={"", "/"}) -仅"/"适用,""无效。但是我发现这是可行的:@RequestMapping(value={"/", " * "}),可以" * "匹配任何内容,因此如果没有其他内容,它将是默认处理程序。

回答3:

如果您仍想获取被调用的uri,则最好使用PathVariable批注。

@PostMapping("/pub/{action:a|b|c}")
public JSONObject handlexxx(@PathVariable String action, @RequestBody String reqStr){
...
}