运行环境新建测试类
package com.wusiyao.websockets.service;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
@Service
public class TimeService {
public String getTime() {
System.out.println("获取时间" + new Date());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
System.out.println("格式化输出:" + sdf.format(d));
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println("Asia/Shanghai:" + sdf.format(d));
return "格式化输出:" + sdf.format(d) + '\n' + "Asia/Shanghai:" + sdf.format(d);
}
}
TimeController:
package com.wusiyao.websockets.controller;
import com.wusiyao.websockets.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TimeController {
@Autowired
TimeService timeService;
@RequestMapping("/time")
public String getTime(){
return timeService.getTime();
}
}