groovy获取当前日期
时间: 2023-09-04 18:15:22 浏览: 142
在Groovy中获取当前日期可以使用`java.time.LocalDate`类。以下是获取当前日期的示例代码:
```groovy
import java.time.LocalDate
LocalDate currentDate = LocalDate.now()
println "当前日期:" + currentDate
```
输出结果类似于:
```
当前日期:2021-11-01
```
你可以根据需要自定义输出格式,例如:
```groovy
import java.time.LocalDate
import java.time.format.DateTimeFormatter
LocalDate currentDate = LocalDate.now()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
String formattedDate = currentDate.format(formatter)
println "当前日期:" + formattedDate
```
输出结果为:
```
当前日期:2021-11-01
```
相关问题
groovy 获取当前时间
在 Groovy 中,可以使用 `java.time` 包中的 `LocalDateTime` 类获取当前时间。示例代码如下:
```groovy
import java.time.LocalDateTime
LocalDateTime now = LocalDateTime.now()
println("当前时间: " + now)
```
输出:
```
当前时间: 2021-09-17T16:25:53.051
```
你可以根据需要对时间进行格式化、加减等操作。
groovy获取post请求
可以使用Groovy的HTTPBuilder库来获取POST请求。以下是一个示例代码:
```groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1' )
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.POST
def http = new HTTPBuilder('http://example.com')
http.request(POST) {
uri.path = '/path/to/resource'
body = [key1: 'value1', key2: 'value2'] // POST请求的数据体
headers.'Content-Type' = 'application/json' // 请求头设置
response.success = { resp, json ->
println "Response status: ${resp.statusLine}"
println "Response data: ${json}"
}
}
```
在上面的代码中,我们使用HTTPBuilder来创建一个HTTP请求,并指定了请求的方法为POST。然后,我们设置了URI的路径和请求体数据,以及请求头的Content-Type。最后,我们定义了处理响应的回调函数,在响应成功时打印响应状态和响应数据。
你可以根据你的实际情况修改以上示例代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)