我有以下代码。我想将变量dateStore
存储在另一个String类型的变量中。我已经彻底搜索了网络但没有找到任何内容,所以我希望你们可以提供帮助!我该怎么做呢?
public String orderTime(){
String dateStore = "";
String time = "";
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
time = timeFormat.format(new Date());
System.out.println ();
dateStore = sdf.format(orderDay.getTime());
}
return "Date of Order: " + dateStore +
"Time of Order: " + time +
"=============================================";
}
基本上,我正在做订单计划。它通过使用orderTime()
方法显示订单的时间。现在,我想在特定日期显示所有订单,所以我认为我必须将dateStore
变量存储到另一个变量,以便我可以在另一个方法中将其用于将其插入Vector中以便以后进行比较上。对不起,如果这不清楚,我想尽力解释。
答案 0 :(得分:2)
如果我理解您的问题,请修改您的方法(或添加其他方法)以返回dateStore
。像
public String orderDate(){
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
return sdf.format(orderDay.getTime());
}
return "";
}
如果您想将日期和时间作为一个整体返回,那么我建议您创建POJO之类的
public class DateAndTime {
final String date;
final String time;
public DateAndTime(String date, String time) {
this.date = date;
this.time = time;
}
@Override
public String toString() {
return "Date of Order: " + date +
"Time of Order: " + time +
"=============================================";
}
}
然后从您的方法返回DateAndTime
实例,如
public DateAndTime orderTime(){
String dateStore = "";
String time = "";
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
time = timeFormat.format(new Date());
// System.out.println ();
dateStore = sdf.format(orderDay.getTime());
}
return new DateAndTime(dateStore, time);
}
因为POJO会覆盖toString()
,您可以打印它并获取原始输出字符串。
答案 1 :(得分:0)
public String[] orderTime(){
String dateStore = "";
String time = "";
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
time = timeFormat.format(new Date());
System.out.println ();
dateStore = sdf.format(orderDay.getTime());
}
String[] aReturn = new String[2];
aReturn[0] = "Date of Order: " + dateStore;
aReturn[1] = "Time of Order: " + time;
return aReturn;
}
然后你可以分别访问这两个字符串值。
答案 2 :(得分:0)
您正在使用面向对象的语言 - 它使得做对象事情变得非常容易。你只需要制作物品并告诉他们该做什么。
public class Order {
// Only create the formatters once.
static final SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
static {
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+1"));
}
// Grab the current date/time at creation time.
final GregorianCalendar orderDay = new GregorianCalendar();
final Date time = new Date();
// What was the date of the order.
public String getDate() {
return sdf.format(orderDay);
}
// What was the time of the oorder.
public String getTime() {
return timeFormat.format(time);
}
@Override
public String toString() {
// A nice representation of the Order.
return "Date of Order: " + getDate()
+ "Time of Order: " + getTime()
+ "=============================================";
}
}
public Order newOrder() {
return new Order();
}
答案 3 :(得分:0)
+1 OldCurmudgeon
您只需要将orderDay变量声明为您的类的成员
public class Order {
private GregorianCalendar orderDay;
...
更改行
GregorianCalendar orderDay = new GregorianCalendar();
通过
this.orderDay = new GregorianCalendar();
并添加此内容。在每个orderDay之前
在时间和时间做同样的事情 在创建getter以获取不同格式的值
之后public GregorianCalendar get_orderDay() {
Return this.orderDay;
}
答案 4 :(得分:-3)
我们可以对这些方法做很多改进,但是关注你的问题,你可以使用输出参数,它更容易阅读和不准备。
public String orderTime(String out dateStore){
dateStore = "";