以下是多种不同的Java实现方法来格式化时间到微秒:

请注意,以下方法中的日期格式"yyyy-MM-dd HH:mm:ss.SSSSSS"可以根据需求进行修改。

"yyyy-MM-dd HH:mm:ss.SSS" 格式化时间到毫秒

"yyyy-MM-dd HH:mm:ss.SSSSSS"格式化时间到微秒

方法一:使用SimpleDateFormat

SimpleDateFormat类可以用来格式化日期和时间

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

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

方法二:使用DateTimeFormatter

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

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
        String formattedDate = now.format(formatter);
        System.out.println(formattedDate);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

方法三:使用Joda-Time库

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

public class Main {
    public static void main(String[] args) {
        DateTime dateTime = DateTime.now();
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
        String formattedDate = dateTime.toString(formatter);
        System.out.println(formattedDate);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.