SimpleDateFormat与DateTimeFormatter的常规应用

本文详细介绍如何使用Java的SimpleDateFormat和DateTimeFormatter进行日期时间的格式化和解析,包括线程安全性和常见的时间格式转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,直接上代码,看懂基本就会用了系列

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtil {
	/*
	 * SimpleDateFormat
	 */
	private static void SimpleDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		Date date = new Date();
		System.out.println(date);
		String str = sdf.format(date);		//Date-String
		System.out.println(str);
		try {
			Date date2 = sdf.parse(str);	//String-Date
			System.out.println(date2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * DateTimeFormatter
	 */    
    //str->Date
    public static Date strToDate(String dateTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
    //Date->str
    public static String dateToStr(Date date,String formatStr){
        if(date == null){
            return null;	//commons-lang3-x.x.x.jar包:return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

/*
	public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";		//常用时间格式
    //str->Date - 采用常用时间格式
    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
    //Date->str - 采用常用时间格式
    public static String dateToStr(Date date){
        if(date == null){
            return null;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }
*/    
    
	public static void main(String[] args) {
		//SimpleDateFormat-非线程安全
		System.out.println("======SimpleDateFormat-非线程安全======");
		SimpleDate();
		
		//DateTimeFormatter-线程安全-导jar包:joda-time-x.x.x.jar
		System.out.println("======DateTimeFormatter-线程安全======");
		System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateTimeUtil.strToDate("2020-08-25 13:14:52","yyyy-MM-dd HH:mm:ss"));
	}
	
}

DateTimeFormatter-线程安全-导jar包:joda-time-x.x.x.jar:https://mvnrepository.com/artifact/joda-time/joda-time

一个较好但“不完整”的介绍:https://www.jianshu.com/p/b212afa16f1f

其他常用的类型转换:

String str;			//默认值为Null,即为空未分配对象
string str = "";	//分配了一个对象引用,其值是个空字符
byte buf = new byte[1024*1024];		//1024*1024:1M
	
	byte->String:	new String(buf)
	String->byte:	str.getBytes()   
	
	int->String:	Integer.toString(int)
					Integer.toHexString(int)
	String->int:	Integer.parseInt(str)
	String->Double:	Double.parseDouble(str)

char[] ans;
	String->char[] 		str.toCharArray()
	char[]->String		String.valueOf(ans)

Arrays.asList(array)
list.toArray()

对象->String(JSON)
	String json = JSON.toJSONString(User);
	User user = JSON.parseObject(json,User.class)

		
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");		//非线程安全
	Date->String:	sdf.format(date)
	String->Date:	sdf.parse(string)
	
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
DateTimeFormatter dtf = new DateTimeFormatter.forPattern("yyyy-MM-dd HH:mm:ss:SSS");		//线程安全
	DateTime dateTime = dtf.parseDateTime(dateTimestr);
	dateTime.toDate();
	DateTime.parse(string,dtf).toDate()
		
SimpleDateTimeFormate
	LocalTime now = LocalTime.now();
	DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mm:ss");
	String t = now.format(format);	
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值