简体   繁体   English

如何在Java中将字符串转换为日历对象

[英]how to convert the a string to a calendar object in java

I have the below string which is input to my method String xymessage="Your item(s) will be ready Today for pickup by 10:00 am "; 我将以下字符串输入到我的方法中字符串xymessage =“您的商品将在今天准备就绪,可以在上午10:00之前领取”;

Now how can i convert this string to a calendar object. 现在如何将这个字符串转换为日历对象。

I was able to extract the day ie. 我能够提取一天,即。 whether its "today" or "tomorrow". 无论是“今天”还是“明天”。 And also the time ie. 还有时间,即。 "10:00 am" using these two parameters as input ie. 使用这两个参数作为输入,即“上午10:00”。 today and 10:00 am will it be possible for me to convert it to a calendar object? 今天和上午10:00是否可以将其转换为日历对象? Sample code snippet: 示例代码段:

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

Use Calendar cal = Calendar.getInstance() to get a calendar object with the current date and time. 使用Calendar cal = Calendar.getInstance()获取具有当前日期和时间的日历对象。 Using the add() , get() , and set() methods, you can set the calendar object correctly. 使用add()get()set()方法,可以正确设置日历对象。 For instance, to change the date to tomorrow's, you could do: cal.add(Calendar.DAY_OF_MONTH, 1); 例如,要将日期更改为明天的日期,您可以执行以下操作: cal.add(Calendar.DAY_OF_MONTH, 1);

To set the hour, cal.set(Calendar.HOUR, hr); 要设置小时, cal.set(Calendar.HOUR, hr);设置cal.set(Calendar.HOUR, hr); where hr was initialized with the hour to be set. hr是使用要设置的小时初始化的。 Similarly for minutes, etc. 同样是几分钟,等等。

You can use SimpleDateFormat for the format you want. 您可以将SimpleDateFormat用作所需的格式。 But as you are having am or pm instead of plain simple AM/PM, it makes a bit of complication. 但是,由于您要使用am或pm而不是简单的简单AM / PM,这使操作变得有些复杂。 Check the below code if it helps for "today" condition : Here variable 'time' is what you have extracted like "10:00 am" 检查下面的代码是否对“今天”情况有所帮助:在这里,变量“时间”是您提取的内容,例如“上午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);
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()));
      }
    }
  }
}

just use SimpleDateFormat("hh:mm aa") 只需使用SimpleDateFormat(“ hh:mm aa”)

Try below code. 尝试下面的代码。

    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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM