我希望将我的日期格式更改为MM/DD/YYYY,目前它在YYYY/MM/DD中。
我试着研究它,但讽刺的是,它总是反过来。现在人们可能会说,试一试,试着从那里开始工作,但是它没有起作用。
我的全班同学给我打电话:
import java.util.*;
import java.text.*;
class Driver {
public static void main (String[] args) {
Kid kid;
Node list = new Node();
kid = createKid("Lexie", 2.6, "11/5/2009");
insertEnd(list, kid);
kid = createKid ("Sally", 2.3, "4/8/2009");
insertEnd(list, kid);
kid = createKid ("Joe", 2.7, "6/16/2009");
insertEnd(list, kid);
kid = createKid ("Bob", 2.2, "1/16/2009");
insertEnd(list, kid);
kid = createKid ("Tom", 3.1, "8/16/2009");
insertEnd(list, kid);
printList(list);
} //end main method
public static Kid createKid(String name, double height, String date) {
return new Kid(name, height, date);
}
} //end class
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.*;
class Kid {
String name;
double height;
GregorianCalendar bDay;
...
/**
* Second constructor for kid
* Setting instances to equal the constructors of this
* @param 1: Setting n (aka name, but it was taken) to equal the instance var of name
* @param 2: Setting h (aka height, but it was taken) to equal the instance var of height
* @param 3: Setting date to equal the instance var of bDay with some modifications
*/
public Kid (String n, double h, String date) {
StringTokenizer st = new StringTokenizer(date, "/");
this.name = n;
this.height = h;
this.bDay = new GregorianCalendar(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
/**
* public String toString() {
* Converting Java language to English language
*/
public String toString() {
return (this.name + ", Height: " + this.height + "ft., Born: "
+ this.bDay.get(Calendar.DATE) + "/" + this.bDay.get(Calendar.MONTH)
+ "/" + this.bDay.get(Calendar.YEAR));
}
} //end class 顺便说一句,我不熟悉的简单日期格式类和日期格式类都没有成功地实现它们。
发布于 2012-06-05 16:56:13
只需使用SimpleDateFormat将String转换为Date即可。不需要用痛苦的Calendar API来麻烦。
String dateString = "2012/06/05";
Date date = new SimpleDateFormat("yyyy/MM/dd").parse(dateString);而是在代码中使用此Date对象。每当您需要将Date对象呈现给人类时,只需使用另一个SimpleDateFormat
String dateString = new SimpleDateFormat("MM/dd/yyyy").format(date);发布于 2016-08-16 22:26:46
tl;dr
String output = LocalDate.parse( "2012/06/05".replace( "/" , "-" ) ).format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.US ) ) ;详细信息
BalusC的回答是正确的,但现在已经过时了。
旧的日期时间类,如Date和Calendar,现在都是遗留的了。使用java.time类代替。
ISO 8601
您的输入字符串几乎符合ISO 8601标准。只需用连字符替换斜杠字符即可。默认情况下,java.time类以ISO8601格式解析/生成字符串,而不需要定义格式模式。
String input = "2012/06/05".replace( "/" , "-" );LocalDate
LocalDate类表示一个日期纯值,没有一天的时间,也没有时区.
LocalDate ld = LocalDate.parse( input );要生成ISO 8601格式的输出,只需调用toString。
String output = ld.toString();若要生成其他格式的字符串,请使用DateTimeFormatter。通常最好通过指定一个Locale来让该类自动本地化。Locale确定了(a)用于翻译的人类语言,(b)大写、标点符号、零件排序等问题的文化规范。
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ) ;
f = f.withLocale( Locale.US ); // Or Locale.CANADA_FRENCH etc.
String output = ld.format( f );关于java.time
java.time框架内置到Java8和更高版本中。这些类取代了旧的麻烦的日期时间类,如java.util.Date、.Calendar和java.text.SimpleDateFormat。
尤达-时间项目,现在在维护模式,建议迁移到java.time。
要了解更多信息,请参见Oracle教程。并搜索堆栈溢出以获得许多示例和解释。
java.time的许多功能都在三次-支持中移植到Java6&7,并进一步适应了ThreeTenABP中的安卓。
三次-额外项目使用其他类扩展java.time。这个项目是将来可能加入java.time的试验场。您可以在这里找到一些有用的类,如Interval、YearWeek、YearQuarter等。
https://stackoverflow.com/questions/10901471
复制相似问题