欢迎访问宙启技术站
智能推送

Java函数实现日期转换成字符串格式

发布时间:2023-06-06 04:28:42

日期转换成字符串格式是Java编程中一个常见的需求。在实际开发中,经常需要将日期转换成指定的字符串格式,用于输出到界面或者存储到数据库中。本文将介绍Java中常用的日期转换成字符串格式的方法。

一、使用SimpleDateFormat类进行日期转换

SimpleDateFormat是Java中常用的日期格式化类,可以使用该类将日期转换成指定格式的字符串。SimpleDateFormat的使用方法如下:

1. 创建SimpleDateFormat对象,指定要转换的日期格式:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

2. 调用SimpleDateFormat的format方法将日期转换成字符串格式:

Date date = new Date();
String str = sdf.format(date);

3. 示例代码:

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

public class DateUtils {

    public static String getDateStr(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static void main(String[] args) {
        Date date = new Date();
        String str = getDateStr(date, "yyyy-MM-dd HH:mm:ss");
        System.out.println(str);
    }
}

二、使用DateTimeFormatter类进行日期转换

Java 8引入了新的日期时间API,其中包含一个DateTimeFormatter类,可以用于将日期转换成指定格式的字符串。DateTimeFormatter的使用方法如下:

1. 创建DateTimeFormatter对象,指定要转换的日期格式:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

2. 调用DateTimeFormatter的format方法将日期转换成字符串格式:

LocalDateTime time = LocalDateTime.now();
String strTime = dtf.format(time);

3. 示例代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    public static String getDateStr(LocalDateTime time, String format) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return dtf.format(time);
    }

    public static void main(String[] args) {
        LocalDateTime time = LocalDateTime.now();
        String strTime = getDateStr(time, "yyyy-MM-dd HH:mm:ss");
        System.out.println(strTime);
    }
}

三、使用String.format方法进行日期转换

String.format方法可以将指定格式的字符串通过占位符%s实现日期格式化。具体使用方法如下:

1. 定义日期格式字符串:

String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS";

其中,%1$tY表示年份,%1$tm表示月份,%1$td表示日,%1$tH表示小时,%1$tM表示分钟,%1$tS表示秒。

2. 调用String.format方法将日期格式化:

Date date = new Date();
String str = String.format(format, date);

3. 示例代码:

import java.util.Date;

public class DateUtils {

    public static String getDateStr(Date date, String format) {
        return String.format(format, date);
    }

    public static void main(String[] args) {
        Date date = new Date();
        String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS";
        String str = getDateStr(date, format);
        System.out.println(str);
    }
}

四、Joda-Time库进行日期转换

Joda-Time是一个流行的日期时间处理库,可以用于将日期转换成指定格式的字符串。Joda-Time的使用方法如下:

1. 创建DateTimeFormatter对象,指定要转换的日期格式:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

2. 调用DateTime的toString方法将日期转换成字符串格式:

DateTime dt = new DateTime();
String strDt = dt.toString(dtf);

3. 示例代码:

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

public class DateUtils {

    public static String getDateStr(DateTime dt, String format) {
        DateTimeFormatter dtf = DateTimeFormat.forPattern(format);
        return dt.toString(dtf);
    }

    public static void main(String[] args) {
        DateTime dt = new DateTime();
        String format = "yyyy-MM-dd HH:mm:ss";
        String strDt = getDateStr(dt, format);
        System.out.println(strDt);
    }
}

总结:

以上四种方法均可以用于将日期转换成指定格式的字符串。其中,SimpleDateFormat是使用最为广泛的日期格式化类,Joda-Time是使用比较广泛的日期时间处理库,DateTimeFormatter是Java 8新的日期时间API中的类,String.format方法则是在一些简单场合下使用的快速方法。在实际开发中,应根据具体需求选择合适的方法进行日期转换成字符串格式。