我有一个名为date的LocalDate变量,当我打印它时显示1988-05-05,我需要将其转换为打印为05。1988年5月。如何做到这一点?
一个很好的方法是使用SimpleDateFormat,我会告诉你怎么做:
SimpleDateFormat sdf = new SimpleDateFormat("d MMMM YYYY");
Date d = new Date();
sdf.format(d);
我看到你有一个变量的日期:
sdf.format(variable_name);
欢呼。
SimpleDateFormat将不能工作,如果他开始与LocalDate,这是新的在Java 8。在我看来,您必须使用DateTimeFormatter, http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html。
LocalDate localDate = LocalDate.now();//For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);
应该是1988年5月5日。要获得日之后月之前的周期,可能必须使用“dd”。LLLL yyyy”
在ProgrammersBlock帖子的帮助下,我想出了这个。我的需求略有不同。我需要获取一个字符串并将其作为LocalDate对象返回。我拿到的代码使用的是旧的Calendar和SimpleDateFormat。我想让它更与时俱进。这是我想到的。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
void ExampleFormatDate() {
LocalDate formattedDate = null; //Declare LocalDate variable to receive the formatted date.
DateTimeFormatter dateTimeFormatter; //Declare date formatter
String rawDate = "2000-01-01"; //Test string that holds a date to format and parse.
dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
//formattedDate.parse(String string) wraps the String.format(String string, DateTimeFormatter format) method.
//First, the rawDate string is formatted according to DateTimeFormatter. Second, that formatted string is parsed into
//the LocalDate formattedDate object.
formattedDate = formattedDate.parse(String.format(rawDate, dateTimeFormatter));
}
希望这能帮助到一些人,如果有人看到了更好的方法来完成这个任务,请添加您的输入。
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));
上面的答案就是今天的答案
在Joda库中有一个内置的方法来格式化LocalDate
import org.joda.time.LocalDate;
LocalDate localDate = LocalDate.now();
String dateFormat = "MM/dd/yyyy";
localDate.toString(dateFormat);
如果你还没有它-添加到build.gradle:
implementation 'joda-time:joda-time:2.9.5'
编码快乐!:)
java.time
不幸的是,所有现有的答案都忽略了一个关键的东西,地点。
日期-时间解析/格式化类型(例如现代API的DateTimeFormatter或遗留API的SimpleDateFormat)是区域敏感的。其模式中使用的符号根据所使用的区域设置打印文本。在没有Locale的情况下,它使用JVM的默认Locale。检查这个答案,了解更多关于它。
文字在预期的输出,05。May 1988是英文的,因此,现有的解决方案只会产生仅仅是巧合的结果(当JVM的默认Locale为英文Locale时)。
使用java解决方案。time,现代date-time API*:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(1988, 5, 5);
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MMMM uuuu", Locale.ENGLISH);
String output = dtf.format(date);
System.out.println(output);
}
}
输出:
05.May 1988
这里,你可以用yyyy代替uuuu,但我更喜欢u而不是y。
从Trail: Date Time了解更多关于现代日期时间API的信息。
*无论出于何种原因,如果你必须坚持使用Java 6或Java 7,你可以使用ThreeTen-Backport,它可以向后移植大部分Java。Java 6和7的时间功能。如果你正在为一个Android项目工作,你的Android API级别仍然不兼容Java-8,检查Java 8+ API通过desugaring和如何使用ThreeTenABP在Android项目。
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件