找到 4330 篇文章 关于 Java 8

Java 程序将 LocalDateTime 格式化为 ISO_WEEK_DATE 格式

Daniol Thomas
更新于 2019-07-30 22:30:25

133 次查看

首先,设置日期:LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20);现在,将日期时间格式化为 ISO_WEEK_DATE 格式:String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);示例import java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 9, 10, 20);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);       System.out.println("Formatted date = "+str);    } }输出DateTime = 2019-09-09T10:20 Formatted date = 2019-W37-1

如何将 Java LocalDateTime 格式化为 ISO_DATE_TIME 格式

Daniol Thomas
更新于 2019-07-30 22:30:25

3K+ 次查看

首先,设置日期:LocalDateTime dateTime = LocalDateTime.of(2019, Month.JULY, 9, 10, 20);现在,将日期时间格式化为 ISO_DATE_TIME 格式:String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);示例import java.time.LocalDateTime; import java.time.Month; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.of(2019, Month.JULY, 9, 10, 20);       System.out.println("DateTime = "+dateTime);       String str = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);       System.out.println("Formatted date = "+str);    } }输出DateTime = 2019-07-09T10:20 Formatted date = 2019-07-09T10:20:00

Java 程序将日期格式化为 Apr 14 2019 01:35 PM IST

Daniol Thomas
更新于 2019-07-30 22:30:25

88 次查看

要格式化和显示日期时间,您需要使用 DateTimeFormatter 并使用以下模式:DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");上面,z 是时区:MMM dd yyyy hh:mm a z现在,对区域使用以下内容:ZonedDateTime dateTime = ZonedDateTime.now();示例import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] argv) {       DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");       ZonedDateTime dateTime = ZonedDateTime.now();       String res = dateTime.format(dtFormat);       System.out.printf("Date = %s %n", res);    } }输出Date = Apr 14 2019 01:35 PM IST

Java 程序将日期格式化为 Apr 19, 2019, 1:27 PM

Daniol Thomas
更新于 2019-07-30 22:30:25

166 次查看

要格式化和显示日期时间,您需要使用 DateTimeFormatter。格式样式为 MEDIUM 和 SHORT:DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);显示格式化的日期:formatter.format(LocalDateTime.now()示例import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; public class Demo {    public static void main(String[] args) {       DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);       System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now()));    } }输出Formatted Date = Apr 19, 2019, 1:27 PM

Java 程序通过 java.nio.file.FileStore 检查支持的属性

Daniol Thomas
更新于 2019-07-30 22:30:25

78 次查看

以下是我们的文件:Path p = Paths.get("E:/input.txt"); FileStore file = Files.getFileStore(p);现在,逐一检查支持的属性:FileAttributeView = file.supportsFileAttributeView(FileAttributeView.class) PosixFileAttributeView = file.supportsFileAttributeView(PosixFileAttributeView.class) BasicFileAttributeView = file.supportsFileAttributeView(BasicFileAttributeView.class)示例import java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.FileOwnerAttributeView; import java.nio.file.attribute.PosixFileAttributeView; public class Demo {    public static void main(String[] args) throws Exception {       Path p = Paths.get("E:/input.txt");       FileStore file = Files.getFileStore(p);       System.out.println("FileAttributeView = " + file.supportsFileAttributeView(FileAttributeView.class));       System.out.println("PosixFileAttributeView = "+ file.supportsFileAttributeView(PosixFileAttributeView.class));       System.out.println("BasicFileAttributeView = "+ file.supportsFileAttributeView(BasicFileAttributeView.class));       System.out.println("FileOwnerAttributeView supported = "+ file.supportsFileAttributeView(FileOwnerAttributeView.class));   ... 阅读更多

Java 程序将列表转换为只读列表

Krantik Chavan
更新于 2019-07-30 22:30:25

152 次查看

假设以下列表不是只读的:List < Integer > list = new ArrayList < Integer > (); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); list.add(20); list.add(40); list.add(50);将上述列表转换为只读:list = Collections.unmodifiableList(list);转换后,您将无法再向列表中添加或删除元素。让我们看一个例子:以下程序将出错,因为我们首先将列表更新为只读,然后尝试从中删除一个元素,这现在是不可能的。原因是我们已将列表转换为只读,您无法再添加或删除元素 ... 阅读更多

如何将 Java 日期显示为“12/04/2019”

Krantik Chavan
更新于 2019-07-30 22:30:25

65 次查看

要格式化并显示像这样的日期,即日/月/年,您需要设置模式:dd/MM/yyyy首先,设置一个 LocalDate:LocalDate localDate = LocalDate.now();现在将日期格式化为“12/04/2019”并显示:localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))示例import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("Date = "+date);       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");       System.out.println("Formatted Date = "+date.format(formatter));    } }输出Date = 2019-04-12 Formatted Date = 12/04/2019

如何在 Java 中格式化和显示日期为“201904”

Krantik Chavan
更新于 2019-07-30 22:30:25

85 次查看

要格式化并显示像这样的日期,即年/月,您需要设置模式:uuuuMM首先,设置一个 LocalDate:LocalDate localDate = LocalDate.now();现在将日期格式化为“201904”并显示:localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))示例import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo {    public static void main(String[] args) {       LocalDate localDate = LocalDate.now();       System.out.println("Date = "+localDate);       System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM")));    } }输出Date = 2019-04-19 Date (Year and Month) = 201904

Java 程序将毫秒转换为可读字符串

Krantik Chavan
更新于 2019-07-30 22:30:25

563 次查看

假设以下是对毫秒的声明:long millis = 5000000;现在从毫秒获取小时、分钟和秒:long hours = TimeUnit.MILLISECONDS.toHours(millis); long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - (hours * 60); long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - ((hours * 60 * 60) + (minutes * 60));使用 String.format() 将以上内容转换为可读字符串。示例import java.util.concurrent.TimeUnit; public class Demo {    public static void main(String[] argv){       long millis = 5000000;       long hours = TimeUnit.MILLISECONDS.toHours(millis);       long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - (hours * 60);       long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - ((hours * ... 阅读更多

Java 程序将 LocalDateTime 转换为 LocalDate 和 LocalTime

Krantik Chavan
更新于 2019-07-30 22:30:25

4K+ 次查看

首先设置一个 LocalDateTime:LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime dateTime = LocalDateTime.of(date, time);现在,将 LocalDateTime 转换为 LocalDate 和 LocalTime:LocalDate localDate = LocalDateTime.now().toLocalDate(); LocalTime localTime = LocalDateTime.now().toLocalTime();示例import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       LocalTime time = LocalTime.now();       LocalDateTime dateTime = LocalDateTime.of(date, time);       System.out.println("DateTime = "+dateTime);       LocalDate localDate = LocalDateTime.now().toLocalDate();       LocalTime localTime = LocalDateTime.now().toLocalTime();       System.out.println("Date = "+localDate);       System.out.println("Time = ... 阅读更多

广告