「java返回时间串」Java 字符串转时间
今天给各位分享java返回时间串的知识,其中也会对Java 字符串转时间进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java 时间串
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class SystemDate {
private static SystemDate systemdate = null;
private Date date;
private String formatType = "yyyy-MM-dd HH:mm";//默认时间格式
private SimpleDateFormat format;
private SystemDate() {}
public static SystemDate getInstance() {
if (systemdate == null)
systemdate = new SystemDate();
return systemdate;
}
//返回当前时间的字符串,用默认时间格式formatType
public String getThisDate() {
return getThisDate(formatType);
}
//返回输入时间的字符串,用默认时间格式formatType
public String getThisDate(Date date) {
return getThisDate(formatType, date);
}
//返回当前时间的字符串,时间格式为输入的参数(String formatType)
public String getThisDate(String formatType) {
date = new Date();
return getThisDate(formatType, date);
}
//返回输入时间的字符串,时间格式为输入的参数(String formatType)
public String getThisDate(String formatType, Date date) {
format = new SimpleDateFormat(formatType);
return format.format(date);
}
//返回时间Date,用输入的时间字符串转化,输入的时间格式为默认时间格式formatType
public Date getAsStringDate(String strdate) throws ParseException {
return getAsStringDate(strdate, formatType);
}
//返回时间Date,用输入的时间字符串转化,输入的时间格式为String formatType
public Date getAsStringDate(String strdate, String formatType) throws
ParseException {
format = new SimpleDateFormat(formatType);
return format.parse(strdate);
}
调用里面的方法可以获得你想要的格式的数据,不明白的地方我再帮你解释
java 返回当前时间
Date date = new Date();
System.out.println(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
哥们,你这样写就可以了
java 编程中显示日期和时间的代码
可以直接通过jdk基本方法,获取到当前的时间
Date date= new Date();//创建一个时间对象,获取到当前的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间显示格式
String str = sdf.format(date);//将当前时间格式化为需要的类型
System.out.println(str);//输出结果
结果为:2015-11-06 13:53:54(实时)。
java返回时间串的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java 字符串转时间、java返回时间串的信息别忘了在本站进行查找喔。
发布于:2022-12-22,除非注明,否则均为
原创文章,转载请注明出处。