SimpleDateFormat与DateTimeFormatter

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

观码时刻

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);	
	
end
  • 作者:suoyue_zhan(联系作者)
  • 发表时间:2020-09-01 22:47:45
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 转载声明:如果是转载栈主转载的文章,请附上原文链接
  • 公众号转载:请在文末添加作者公众号二维码(公众号二维码见右边,欢迎关注)
  • 评论