gpt4 book ai didi

java - 更改 YYYY/MM/DD -> MM/DD/YYYY java

转载 作者:行者123 更新时间:2023-12-02 02:56:09 25 4
gpt4 key购买 nike

我希望将日期格式更改为 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

顺便说一句,简单日期格式类和日期格式类我不熟悉并且尝试实现它们没有成功。

最佳答案

只需使用 SimpleDateFormat字符串转换为日期。无需为痛苦的 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);

关于java - 更改 YYYY/MM/DD -> MM/DD/YYYY java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com