如何只存储 return 语句的一部分
How to store just a part of a return statement
我有以下代码。我只想将变量 dateStore
存储在另一个字符串类型的变量中。网上查了好久都没找到,希望大家多多指教!请问我该怎么做呢?
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 中,以便稍后进行比较上。抱歉,如果这不是很清楚,我会尽力解释。
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;
}
然后就可以分别访问这两个字符串值了。
如果我理解您的问题,请将您的方法(或添加其他方法)修改为 return dateStore
。像
public String orderDate(){
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
return sdf.format(orderDay.getTime());
}
return "";
}
如果你想return以日期和时间为单位,那么我建议你创建一个POJO like
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 +
"=============================================";
}
}
然后 return 来自您的方法的 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()
你可以打印它并得到你的原始输出字符串。
我们可以对这些方法做很多改进,但是针对您的问题,您可以使用输出参数,这样更容易阅读和理解。
public String orderTime(String out dateStore){
dateStore = "";
您正在使用面向对象的语言 - 它使处理对象的事情变得非常容易。您只需要制作对象并告诉它们该做什么。
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();
}
+1 到老脾气
您只需将 orderDay 变量声明为 class
的成员
public class Order {
private GregorianCalendar orderDay;
...
换行
GregorianCalendar orderDay = new GregorianCalendar();
来自
this.orderDay = new GregorianCalendar();
并添加这个。在每个订单日之前
时间和
在创建 getter 以获取不同格式的值后
public GregorianCalendar get_orderDay() {
Return this.orderDay;
}
我有以下代码。我只想将变量 dateStore
存储在另一个字符串类型的变量中。网上查了好久都没找到,希望大家多多指教!请问我该怎么做呢?
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 中,以便稍后进行比较上。抱歉,如果这不是很清楚,我会尽力解释。
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;
}
然后就可以分别访问这两个字符串值了。
如果我理解您的问题,请将您的方法(或添加其他方法)修改为 return dateStore
。像
public String orderDate(){
if(!out){
GregorianCalendar orderDay = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd/MM/yyyy");
return sdf.format(orderDay.getTime());
}
return "";
}
如果你想return以日期和时间为单位,那么我建议你创建一个POJO like
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 +
"=============================================";
}
}
然后 return 来自您的方法的 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()
你可以打印它并得到你的原始输出字符串。
我们可以对这些方法做很多改进,但是针对您的问题,您可以使用输出参数,这样更容易阅读和理解。
public String orderTime(String out dateStore){
dateStore = "";
您正在使用面向对象的语言 - 它使处理对象的事情变得非常容易。您只需要制作对象并告诉它们该做什么。
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();
}
+1 到老脾气
您只需将 orderDay 变量声明为 class
的成员public class Order {
private GregorianCalendar orderDay;
...
换行
GregorianCalendar orderDay = new GregorianCalendar();
来自
this.orderDay = new GregorianCalendar();
并添加这个。在每个订单日之前
时间和 在创建 getter 以获取不同格式的值后
public GregorianCalendar get_orderDay() {
Return this.orderDay;
}