Java 转换不规则日期

Java 转换不规则日期,java,datetime,jodatime,Java,Datetime,Jodatime,我有一张不定期约会的清单,如下所示: 2005年9月29日星期四17:52:45格林尼治标准时间 2005年8月17日星期三21:21:08+0200 2005年8月17日星期三20:08:22+0200 2005年8月15日星期一21:44:07+0200 2005年7月24日星期日21:47:09+0200 2005年7月24日星期日12:37:46-0700 2005年7月24日星期日21:37:51+0200 2005年7月11日星期一21:19:38+0200 2005年7月11日星期

我有一张不定期约会的清单,如下所示:

2005年9月29日星期四17:52:45格林尼治标准时间

2005年8月17日星期三21:21:08+0200

2005年8月17日星期三20:08:22+0200

2005年8月15日星期一21:44:07+0200

2005年7月24日星期日21:47:09+0200

2005年7月24日星期日12:37:46-0700

2005年7月24日星期日21:37:51+0200

2005年7月11日星期一21:19:38+0200

2005年7月11日星期一21:19:02+0200

2005年7月11日星期一20:43:08+0200(CEST)

2006年11月13日14:06:20+0000


如何使用JodaTime或默认java date类将它们转换为DateTime或just Time?(乔达时间优先)

每一个都遵循一定的原则。准备一组预定义的DateFormat处理程序,并将日期字符串传递到链中。让适当的处理程序解析日期并为您返回日期


注意:这是假设您发布的日期是字符串格式的。如果您有一个java.util.Date对象,那么我相信您看到的是显示时间格式。

我不知道如何使用jodatime,但您可以使用

字符串模式=“MM/dd/yyyy”


SimpleDataFormat格式=新的SimpleDataFormat(模式)

Pangea当我读到你的消息时,我太累了,我想发布解决方案,然后我又读了回来,现在我意识到,如果尝试使用空返回或尝试其他模式,我可以做得更轻松

无论如何,这是一个艰难的解决方案:

String[] days = {
  "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};
String[] months = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
String[] years = {
  "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"
};


// ORDER IS VERRY IMPORTANT!! ( MEST must be before EST for example)
String[] timesZones = {
  "BST", "CET", "CEST", "CST", "CDT", "EDT", "GMT+00:00", "GMT", "IST", "MEST", "EST", "MET", "MDT", "PST", "PDT", "SAST", "UTC", "UT", "W. Europe Standard Time", "West-Europa (zomertijd)"
};

DateTime handleDate(String date) {

  String origDate = date;

  String timeZone = "";
  String year = "";
  String month = "";
  String day = "";
  int hours = 0;
  int minutes = 0;
  int seconds = 0;

  // is it valid?
  date = trim(date);
  if (date.equals("")) {
    return null;
  }

  // first delete the comma that comes mostly after the day
  date = date.replaceAll(",", "");

  // remove the day
  for (int i = 0; i < days.length; i++) {
    if (date.contains(days[i])) {
      date = date.replace(days[i], "");
      break;
    }
  }


  // if(date.contains("23:27:17")) println(date);

  for (int i = 0; i < timesZones.length; i++) {
    // first check with '(' and ')'
    String target = "("+timesZones[i]+")";

    if (date.contains(target)) {      
      timeZone = timesZones[i];
      date = date.replace(target, "");
      break;
    }

    // if not found check without '(' and ')'
    if (date.contains(timesZones[i])) {      
      timeZone = timesZones[i];
      date = date.replace(timesZones[i], "");
      break;
    }
  }

  // get the month
  for (int i = 0; i < months.length; i++) {
    if (date.contains(months[i])) {
      month = months[i].toLowerCase(); // !must be lowercase
      // must be dutch on my pc
      if(month.equals("oct")) month = "okt";
      if(month.equals("may")) month = "mei";
      if(month.equals("mar")) month = "mrt";

      date = date.replace(months[i], "");
      break;
    }
  }

  // get the year
  for (int i = 0; i < years.length; i++) {
    if (date.contains(years[i])) {
      year = years[i];
      date = date.replace(years[i], "");
      break;
    }
  }

  // get the time
  Pattern p = Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)");
  Matcher m = p.matcher(date);
  if (m.find()) {
     // also fix the time, 00 is not allowed
    hours = int(m.group(1));
    minutes = int(m.group(2));
    seconds = int(m.group(3));

    date = date.replaceAll("(\\d\\d:\\d\\d:\\d\\d)", "");
  }

  // get the time difference
  date = date.replace("+-", "+0"); // bug fix where data is incorrect ( 16 Sep 2007 23:27:17 +-200)

  p = Pattern.compile("[+|-]*(\\d\\d)\\d\\d");
  m = p.matcher(date);
  if (m.find()) {
    int timeDifferenceH = int(m.group(1));

    date = date.replaceAll("([+|-]*\\d\\d\\d\\d)", "");
  }

  date = " "+date; // bug fix

  // get the day
  for (int i = 31; i >= 1; i--) {
    // first check for the ones that contains 2 digits (like 07)
    String d = nf(i, 2);
    if (date.contains(d)) {
      day = nf(i, 2);
      date = date.replace(d, "");
      break;
    }

    // check for 1 digit
    d = ""+i;
    if (date.contains(d)) {
      day = nf(i, 2);
      date = date.replace(d, "");
      break;
    }
  }


  // there should be nothing left except white space
  date = date.replace(" ", "");
  if (date.equals("") == false) {
    println("handleDate: problem with input\n"+date);
    println(origDate+"\n");
    println(year);
    println(month);
    println(day);
  }


  String cleanDate = day+"/"+month+"/"+year+" "+nf(hours, 2)+":"+nf(minutes, 2)+":"+nf(seconds, 2);

  DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MMM/yyyy HH:mm:ss");

  try {
      DateTime dt = formatter.parseDateTime(cleanDate);
      return dt;
    } catch (IllegalArgumentException iae) {
      println("handleDate: Problem with formatting: "+cleanDate);
    }

  return null;  
}
String[]天={
“星期一、星期二、星期三、星期四、星期五、星期六、星期日”
};
字符串[]个月={
“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
};
字符串[]年={
"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"
};
//秩序非常重要!!(例如,MEST必须在EST之前)
字符串[]时区={
“BST”、“CET”、“CEST”、“CST”、“CDT”、“EDT”、“GMT+00:00”、“GMT”、“IST”、“MEST”、“EST”、“MET”、“MDT”、“PST”、“PDT”、“SAST”、“UTC”、“UT”、“西欧标准时间”、“西欧罗巴(zomertijd)”
};
DateTime handleDate(字符串日期){
字符串origDate=日期;
字符串时区=”;
字符串年份=”;
字符串月份=”;
字符串日期=”;
整小时=0;
整数分钟=0;
整数秒=0;
//它有效吗?
日期=修剪(日期);
如果(日期等于(“”){
返回null;
}
//首先删除一天之后的逗号
date=date.replaceAll(“,”和“”);
//取消这一天
对于(int i=0;i=1;i--){
//首先检查包含2个数字的数字(如07)
字符串d=nf(i,2);
如果(日期包含(d)){
日=nf(i,2);
日期=日期。替换(d,“”);
打破
}
//检查是否有1位数字
d=”“+i;
如果(日期包含(d)){
日=nf(i,2);
日期=日期。替换(d,“”);
打破
}
}
//除了空白,应该什么都没有了
日期=日期。替换(“,”);
如果(日期等于(“”==false){
println(“handleDate:输入有问题\n”+日期);
println(origDate+“\n”);
println(年);
println(月);
println(日);
}
字符串cleanDate=day+“/”+month+“/”+year+“+nf(小时,2)+”:“+nf(分钟,2)+”:“+nf(秒,2);
DateTimeFormatter formatter=DateTimeFormat.forPattern(“dd/MMM/yyyy HH:mm:ss”);
试一试{
DateTime dt=格式化程序.parseDateTime(cleanDate);
返回dt;
}捕获(IllegalArgumentException iae){
println(“handleDate:格式问题:“+cleanDate”);
}
返回null;
}

它们是
String
s还是
Date
s?