java 当前日期 格式化为yyyy-mm-dd 保证线程安全 jdk1.6环境
时间: 2023-09-22 18:08:08 浏览: 49
在 JDK 1.6 环境中,可以使用 `DateFormat` 类来格式化日期,并使用 `ThreadLocal` 来实现线程安全。以下是将当前日期格式化为 "yyyy-MM-dd" 格式的 Java 代码示例:
```java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
private static final ThreadLocal<DateFormat> dateFormatThreadLocal = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static void main(String[] args) {
String formattedDate = dateFormatThreadLocal.get().format(new Date());
System.out.println(formattedDate);
}
}
```
这样每个线程都会拥有一个独立的 `DateFormat` 实例,避免了线程安全问题。
相关问题
用jdk1.8的方法判断字符串是否是yyyy-MM-dd HH:mm:ss还是yyyy-MM-dd HH:mm 都统一转成yyyy-MM-dd HH:mm
可以使用正则表达式来判断字符串是否符合指定的格式。具体实现如下:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatTest {
public static void main(String[] args) {
String pattern = "yyyy-MM-dd HH:mm";
String dateString = "2022-01-01 12:00:00";
// 判断字符串是否符合 yyyy-MM-dd HH:mm:ss 格式
if (dateString.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
pattern = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat inputFormat = new SimpleDateFormat(pattern);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = inputFormat.parse(dateString);
String outputString = outputFormat.format(date);
System.out.println(outputString);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
首先定义默认格式为 yyyy-MM-dd HH:mm,然后判断字符串是否符合 yyyy-MM-dd HH:mm:ss 格式,如果符合则更新格式为 yyyy-MM-dd HH:mm:ss。
接着使用 SimpleDateFormat 类将字符串解析为 Date 对象,再将 Date 对象按照默认格式转换为字符串输出。
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 是一个用于格式化日期的类,其中 "yyyy-MM-dd" 是日期的格式。这个格式表示日期的年份是四位数,月份和日期都是两位数。 通过使用这个格式,可以将日期对象转换为字符串,或者将字符串转换为日期对象。这个类提供了一些方法,如format()用于将日期对象格式化为指定格式的字符串,parse()用于将字符串解析为日期对象。它还可以处理时区和本地化等问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式](https://blog.csdn.net/weixin_30341735/article/details/96249028)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [JDK8及以上版本的odbc驱动](https://download.csdn.net/download/zdsdiablo/15682324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [时间戳与日期相互转化和简单计算(附有实例)SimpleDateFormat df = new SimpleDateFormat(“yyyy-...](https://blog.csdn.net/qq_45064695/article/details/120899657)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]