GregorianCalendar 输出:日期是 java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true
GregorianCalendar outputs: The date is java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true
我在一个文本文件中有以下数据,稍后我将扫描其中。
17-Mar-2006 1100 1280
16-Jan-2002 1120 1230
15-Jun-2003 1140 1900
我正在将此数据构建为 "Stockmarket" 对象。
实例变量 "date" 声明为 GregorianCalendar。
构造函数 String dt 应在 String 中采用 "dd-MMM-yyyy" 形式。
public String date() 也是如此,它应该是 return 形式为 "dd-MMM-yyyy" 的字符串。
我通过以下方式完成,但输出:
日期是 java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=".....
有解决办法吗?
public class Stockmarket{
// instance variables
private GregorianCalendar date;
private double opening;
private double closing;
public Stockmarket(String dt, double opening, double closing)
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date d = sdf.parse(dt);
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
cal.setTime(d);
this.date=cal;
this.opening=opening;
this.closing=closing;
}
public String date(){
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy, Locale.US");
df.setCalendar(this.date);
String date = df.format(this.date.getTime());
return date;
}
如果格式已知且统一,您可以使用 SimpleDateFormat 实现您想要的。
public class TestParse {
public static void main(String[] args) {
String myDate = "17-Mar-2006";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;
try {
date = simpleDateFormat.parse(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar.getTime());
}
}
编辑:
根据问题变化添加了更多信息
方法 getStringFromDate
生成 17-Mar-2006
的格式化日期
public class TestParse {
public static void main(String[] args) {
String myDate = "17-Mar-2006";
Date date = getDateFromString(myDate);
// Set the Calendar
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
// format date
String formattedDate = getStringFromDate(calendar);
System.out.printf("The formatted date is : %s & the dates are equal %s%n", formattedDate, formattedDate.equals(myDate));
}
public static String getStringFromDate(GregorianCalendar calendar) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
simpleDateFormat.setCalendar(calendar);
return simpleDateFormat.format(calendar.getTime());
}
public static Date getDateFromString(String input) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date date = null;
try {
date = simpleDateFormat.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
您的股票市场class格式固定类型
public class Stockmarket {
// instance variables
private GregorianCalendar date;
private double opening;
private double closing;
public Stockmarket(String dt, double opening, double closing) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date d = null;
try {
d = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
cal.setTime(d);
this.date = cal;
this.opening = opening;
this.closing = closing;
}
public String date() {
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
df.setCalendar(this.date);
String date = df.format(this.date.getTime());
return date;
}
}
测试Class
public class TestParse {
public void doTest()
{
Stockmarket stockmarket1 = new Stockmarket("17-Mar-2006",500,600);
stockmarket1.date();
System.out.println(stockmarket1.date());
Stockmarket stockmarket2 = new Stockmarket("16-Jan-2002",500,600);
stockmarket2.date();
System.out.println(stockmarket2.date());
Stockmarket stockmarket3 = new Stockmarket("15-Jun-2003",500,600);
stockmarket3.date();
System.out.println(stockmarket3.date());
}
public static void main(String[] args) {
TestParse testParse = new TestParse();
testParse.doTest();
}
}
我在一个文本文件中有以下数据,稍后我将扫描其中。
17-Mar-2006 1100 1280
16-Jan-2002 1120 1230
15-Jun-2003 1140 1900
我正在将此数据构建为 "Stockmarket" 对象。 实例变量 "date" 声明为 GregorianCalendar。 构造函数 String dt 应在 String 中采用 "dd-MMM-yyyy" 形式。 public String date() 也是如此,它应该是 return 形式为 "dd-MMM-yyyy" 的字符串。 我通过以下方式完成,但输出: 日期是 java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=".....
有解决办法吗?
public class Stockmarket{
// instance variables
private GregorianCalendar date;
private double opening;
private double closing;
public Stockmarket(String dt, double opening, double closing)
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date d = sdf.parse(dt);
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
cal.setTime(d);
this.date=cal;
this.opening=opening;
this.closing=closing;
}
public String date(){
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy, Locale.US");
df.setCalendar(this.date);
String date = df.format(this.date.getTime());
return date;
}
如果格式已知且统一,您可以使用 SimpleDateFormat 实现您想要的。
public class TestParse {
public static void main(String[] args) {
String myDate = "17-Mar-2006";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;
try {
date = simpleDateFormat.parse(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar.getTime());
}
}
编辑: 根据问题变化添加了更多信息
方法 getStringFromDate
生成 17-Mar-2006
public class TestParse {
public static void main(String[] args) {
String myDate = "17-Mar-2006";
Date date = getDateFromString(myDate);
// Set the Calendar
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
// format date
String formattedDate = getStringFromDate(calendar);
System.out.printf("The formatted date is : %s & the dates are equal %s%n", formattedDate, formattedDate.equals(myDate));
}
public static String getStringFromDate(GregorianCalendar calendar) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
simpleDateFormat.setCalendar(calendar);
return simpleDateFormat.format(calendar.getTime());
}
public static Date getDateFromString(String input) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date date = null;
try {
date = simpleDateFormat.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
您的股票市场class格式固定类型
public class Stockmarket {
// instance variables
private GregorianCalendar date;
private double opening;
private double closing;
public Stockmarket(String dt, double opening, double closing) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date d = null;
try {
d = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
cal.setTime(d);
this.date = cal;
this.opening = opening;
this.closing = closing;
}
public String date() {
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
df.setCalendar(this.date);
String date = df.format(this.date.getTime());
return date;
}
}
测试Class
public class TestParse {
public void doTest()
{
Stockmarket stockmarket1 = new Stockmarket("17-Mar-2006",500,600);
stockmarket1.date();
System.out.println(stockmarket1.date());
Stockmarket stockmarket2 = new Stockmarket("16-Jan-2002",500,600);
stockmarket2.date();
System.out.println(stockmarket2.date());
Stockmarket stockmarket3 = new Stockmarket("15-Jun-2003",500,600);
stockmarket3.date();
System.out.println(stockmarket3.date());
}
public static void main(String[] args) {
TestParse testParse = new TestParse();
testParse.doTest();
}
}