我有以下字符串输入到我的方法 String xymessage =“你的物品准备就绪,今天上午10点准备好”;
现在我该如何将此字符串转换为日历对象。
我能够提取当天即。无论是“今天”还是“明天”。还有时间即。 “10:00 AM。” 使用这两个参数作为输入即。今天和上午10点我可以将它转换为日历对象吗? 示例代码段:
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
}
}
答案 0 :(得分:0)
使用Calendar cal = Calendar.getInstance()
获取包含当前日期和时间的日历对象。使用add(),get()和set()方法,您可以正确设置日历对象。例如,要将日期更改为明天,您可以执行:cal.add(Calendar.DAY_OF_MONTH, 1);
设置小时cal.set(Calendar.HOUR, hr);
,其中hr已初始化,并设置小时。同样的分钟等等。
答案 1 :(得分:0)
您可以将SimpleDateFormat用于所需的格式。但是你正好在上午或下午。而不是简单的简单AM / PM,它有点复杂。 检查以下代码是否有助于“今天”状况: 这里变量'time'就是您提取的内容,如“上午10点”。
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = new Date();
String timeArray[]=time.split(" ");
String minArray[]=timeArray[0].split(":");
date.setHours(Integer.parseInt(minArray[0]));
date.setMinutes(Integer.parseInt(minArray[1]));
if(!timeArray[1].startsWith("a")){
date.setHours(date.getHours()+12);
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
答案 2 :(得分:0)
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()));
}
}
}
}
只需使用SimpleDateFormat(" hh:mm aa")
答案 3 :(得分:0)
尝试以下代码。
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));
}