java 时间与字符串之间的转换

1. long字符串转换成yyyy-MM-dd HH:mm:ss格式输出

import java.text.SimpleDateFormat;
import java.util.Date;
//将long字符串转换成格式时间输出
public class LongToString {
public static void main(String argsp[]){
	String time="1256006105375";

	Date date=new Date(Long.parseLong(time));
	SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	time=formatter.format(date);
	System.out.println(time);
}
}

2. 字符串转换成时间

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

import ognl.ParseException;

public class StringToDate {
public static void main(String argsp[]) throws Exception{
	String time="2010-11-20 11:10:10";

	Date date=null;
	SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	date=formatter.parse(time);
	System.out.println(date);
}
}
3. 取得当前系统时间,返回yyyy-MM-dd HH:mm:ss字符串

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

public class StringToDate {
public static void main(String argsp[]) throws Exception{
	
	Date date=new Date();
	SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String time=formatter.format(date);
	System.out.println(time);
}
}
4. 取得当前系统时间,返回 HH:mm:ss字符串
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {
public static void main(String argsp[]) throws Exception{
	
	Date date=new Date();
	SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");
	String time=formatter.format(date);
	System.out.println(time);
}
}
5.将20101125102503转换成2010-11-25 10:25:03输出

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

public class StringToDate {
public static void main(String argsp[]) throws Exception{
	
	String time="20101125102503";
	SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");
	SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");
	time=formatter1.format(formatter2.parse(time));
	System.out.println(time);
}
}





©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页