0

我将以下字符串输入到我的方法中 String xymessage="Your item(s) will be ready Today for Pick up by 10:00 am";

现在如何将此字符串转换为日历对象。

我能够提取这一天,即。无论是“今天”还是“明天”。还有时间,即。“10:00 am”使用这两个参数作为输入,即。今天和上午 10:00 我可以将其转换为日历对象吗?示例代码片段:

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
    }  
}
4

4 回答 4

0

用于Calendar cal = Calendar.getInstance()获取具有当前日期和时间的日历对象。使用add()get()set()方法,您可以正确设置日历对象。例如,要将日期更改为明天的,您可以执行以下操作:cal.add(Calendar.DAY_OF_MONTH, 1);

设置小时,cal.set(Calendar.HOUR, hr);其中 hr 使用要设置的小时进行初始化。类似的分钟等。

于 2013-03-14T06:46:18.637 回答
0

您可以将 SimpleDateFormat 用于您想要的格式。但是当您使用 am 或 pm 而不是简单的 AM/PM 时,它会有点复杂。如果对“今天”条件有帮助,请检查以下代码:这里的变量“时间”是您提取的内容,例如“上午 10:00”

    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);
于 2013-03-14T06:58:57.930 回答
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")

于 2013-03-14T07:01:38.607 回答
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));
}
于 2013-03-14T07:09:39.873 回答