一、Date转String
在Java编程中,经常需要将Date类型数据转换为String类型。Date转String的过程可以通过SimpleDateFormat这个工具类来实现。SimpleDateFormat类主要用于将日期格式化为文本字符串和将文本字符串解析为日期。
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringExample {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate= formatter.format(date);
System.out.println("Date converted to String: " + strDate);
}
}
在上述代码中,我们首先创建了一个Date对象,然后使用SimpleDateFormat类创建一个格式化对象formatter。格式化对象通过format()方法将Date对象格式化为String类型的日期。
二、MySQL Date转String
MySQL数据库中日期数据类型为Date、Time、DateTime等,其中Date类型数据格式为YYYY-MM-DD。当我们需要将MySQL中的Date类型数据转为String类型时,可以使用ResultSet接口中的getString()方法获取结果集中的指定列,将其作为String类型返回。
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
try {
con = DriverManager.getConnection(url,user,password);
pst=con.prepareStatement("select dob from employee where id=?");
pst.setInt(1,1);
rs=pst.executeQuery();
String strDate = rs.getString(1);
System.out.println("MySQL Date converted to String: " + strDate);
} catch (SQLException ex) {
ex.printStackTrace();
} finally{
try {
if(rs!=null)rs.close();
if(pst!=null)pst.close();
if(con!=null)con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
在上述代码中,我们使用了JDBC连接MySQL数据库,并从employee表中获取id为1的员工的出生日期dob。然后使用getString()方法获取结果集中的dob列的值,将其转换为String类型的日期。
三、Date类型转String
除了使用SimpleDateFormat类和ResultSet接口中的getString()方法来转换Date类型数据为String类型,我们还可以使用其他的方法来实现。以下是一种将Date类型转换为String类型的另一种方法,其原理是利用Java 8中的新特性,将Date类型转化为Instant类型,然后根据指定的时区转换为LocalDateTime类型,最后通过DateTimeFormatter将LocalDateTime类型转化为String类型。
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class DateToStringExample {
public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strDate= ldt.format(formatter);
System.out.println("Date converted to String: " + strDate);
}
}
在上述代码中,我们首先获取当前时间的Date对象,在将其转化为Instant类型,再转化为LocalDateTime类型,最后通过DateTimeFormatter将LocalDateTime类型转化为String类型。
四、Java 8日期时间API将Date类型转为String类型
Java 8中的DateTimeFormatter类提供了方便的方法将日期时间格式化表示为字符串。我们可以使用该类来将Date类型转为String类型。
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class DateToStringExample {
public static void main(String[] args) {
Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
String strDate = formatter.format(instant);
System.out.println("Date converted to String: " + strDate);
}
}
在上述代码中,我们首先获取当前时间的Date对象,然后将其转化为Instant类型。DateTimeFormatter类提供了ofPattern()方法来指定日期格式,withZone()方法来指定时区。最后,使用format()方法将Instant类型的日期格式化为String类型的日期。这种方法比较简洁,是Java 8中推荐使用的一种方式。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/220066.html
微信扫一扫
支付宝扫一扫