如何在java中将字符串转换为日历对象
我有下面的字符串,它是我的方法的输入 String xymessage=“您的物品将于今天上午10:00前准备好领取” 现在如何将该字符串转换为日历对象 我能够提取当天的信息,无论是“今天”还是“明天”。还有时间,比如“上午10点” 使用这两个参数作为输入,即今天和上午10:00,我是否可以将其转换为日历对象? 示例代码段:如何在java中将字符串转换为日历对象,java,calendar,Java,Calendar,我有下面的字符串,它是我的方法的输入 String xymessage=“您的物品将于今天上午10:00前准备好领取” 现在如何将该字符串转换为日历对象 我能够提取当天的信息,无论是“今天”还是“明天”。还有时间,比如“上午10点” 使用这两个参数作为输入,即今天和上午10:00,我是否可以将其转换为日历对象? 示例代码段: String xymessage="Your item(s) will be ready Today for pickup by 10:00 a.m. ";
String xymessage="Your item(s) will be ready Today for pickup by 10:00 a.m. ";
if(null != xyMessage){
//removing empty spaces.
xyMessage=xyMessage.trim();
LOGGER.debug("sellerId:"+delivSeller.getSellerId()+" and xymessage:"+xyMessage);
if(xyMessage.contains("Today")){
//this means its today
String[] xyArray = xyMessage.split("pickup by");
if(xyArray.length == 2){
String timeVal=xyArray[1];
}
}else{
//this means its tomorrow
}
}
使用
Calendar cal=Calendar.getInstance()
获取具有当前日期和时间的日历对象。使用、和方法,可以正确设置日历对象。例如,要将日期更改为明天,您可以执行以下操作:cal.add(Calendar.DAY\u OF\u MONTH,1)代码>
要设置小时,cal.set(Calendar.hour,hr)代码>其中hr已初始化为要设置的小时数。类似地,对于分钟等,您可以使用SimpleDataFormat作为所需的格式。但是,由于您使用的是上午或下午,而不是简单的上午/下午,因此会有点复杂。
如果“今天”条件有帮助,请检查以下代码:
这里的变量“time”是您提取的“10:00 a.m.”
只需使用SimpleDataFormat(“hh:mmAA”)试试下面的代码
String val = "10:00 a.m";
val = val.replace(".", "");
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Calendar temp = Calendar.getInstance();
temp.setTime(dateFormat.parse(val));
Calendar cal = Calendar.getInstance();
if ("tomorrow".equalsIgnoreCase("YOUR_STRING")) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
cal.set(Calendar.HOUR_OF_DAY, temp.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, temp.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.DAY_OF_MONTH) + ":"
+ (cal.get(Calendar.MONTH) + 1) + ":"
+ cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE));
}
您将如何处理这样的场景:“您的物品将在后天上午10:00准备好取货”@尼拉,我不明白你的问题。你能详细说明吗?正如Uba所建议的,日历有设置/获取/添加(或反向减去)日历的方法。我的评论所指的是字符串的格式。。。这个字符串从何而来:系统生成,用户输入。。。如果是系统生成的,那么为什么不直接将取货日期和时间发送到此代码,这样您只需要使用SimpleDataFormat。如果是用户输入,那么您将如何处理所有场景和可能的消息格式,在这种情况下,我宁愿让用户指定日期(和时间),并让您的代码从中生成消息。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat finalFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
Calendar today = Calendar.getInstance();
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
String xyMessage = "Your item(s) will be ready Today for pickup by 10:00 a.m. ";
if (null != xyMessage) {
//removing empty spaces.
xyMessage = xyMessage.trim();
if (xyMessage.contains("Today")) {
//this means its today
String[] xyArray = xyMessage.split("pickup by ");
String time = xyArray[1].replace(".", "");
today.set(Calendar.HOUR_OF_DAY, sdf.parse(time).getHours());
System.out.println("calendar:" + finalFormat.format(today.getTime()));
} else {
//this means its tomorrow
String[] xyArray = xyMessage.split("pickup by ");
String time = xyArray[1].replace(".", "");
tomorrow.set(Calendar.HOUR_OF_DAY, sdf.parse(time).getHours());
System.out.println("calendar:" + finalFormat.format(tomorrow.getTime()));
}
}
}
}
String val = "10:00 a.m";
val = val.replace(".", "");
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Calendar temp = Calendar.getInstance();
temp.setTime(dateFormat.parse(val));
Calendar cal = Calendar.getInstance();
if ("tomorrow".equalsIgnoreCase("YOUR_STRING")) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
cal.set(Calendar.HOUR_OF_DAY, temp.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, temp.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.DAY_OF_MONTH) + ":"
+ (cal.get(Calendar.MONTH) + 1) + ":"
+ cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE));
}