如何在Java中以以下格式获取日期和时间?

huangapple go评论75阅读模式
英文:

How to get date and time in below format in java?

问题

Sure, here's the translation:

如何获取当前日期和时间,并将其转换为以下字符串格式?

06-04-2020 05:00 PM

之后我需要在日期上加上几分钟?请帮忙!

英文:

How can I get the current date and time and how can I transfer it into the following String format?

06-04-2020 05:00 PM

After that I need to add few minutes to the date? Please help!

答案1

得分: 4

String dateTimePattern = "dd-MM-yyyy HH:mm a";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern);
LocalDateTime yourDay = LocalDateTime.of(2020, 4, 6, 17, 00);
System.out.println(formatter.format(ZonedDateTime.of(yourDay, ZoneId.of("UTC-5"))));
英文:

Your answer should look something like this:

String dateTimePattern = "dd-MM-yyyy HH:mm a";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern );
LocalDateTime yourDay= LocalDateTime.of(2020, 4, 6, 17, 00);
System.out.println(formatter.format(ZonedDateTime.of(yourDay, ZoneId.of("UTC-5"))));

Please look this up for more info on the available formats.

答案2

得分: 2

Date yourDate = new Date();  
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy h:m a");  
String formatedDate = sdf.format(yourDate);
英文:
Date yourDate = new Date();  
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy h:m a");  
String formatedDate= sdf.format(yourDate);

huangapple
  • 本文由 发表于 2020年4月6日 19:22:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61058640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定