2

可以使用例如 SimpleDateFormat(formatPattern, locale) 来格式化日期。是否有可能在给定日期的情况下确定由 formatPattern 表示的时间段(以秒为单位)?例如,如果我们有

Date date = new Date(1286488800);
String formatPattern = "yyyy";

是否可以确定由 formatPattern 表示的年份的长度(以秒为单位)以及日期位于哪个日期?

4

2 回答 2

0

我认为使用 formatPattern 来检测日期范围是一件坏事。您需要为日期模式编写解析器。一个更好的主意是使用具有可能范围(年、月、周等)的下拉列表。那么以秒为单位计算当前范围的长度就不是问题了。

于 2010-10-08T22:19:53.163 回答
0

我相信已经找到了一个合适的解决方案。它对我有用,虽然我不确定也没有测试这是否在每种情况下都准确有效(例如闰秒)。如果您有改进建议,请随时发布。这是代码:

public long getIntervalTimeForFormat(String formatPattern, TimeZone timezone, Locale locale, Date inputDate){
  Date someOddestDate = new Date(1318352124368L);
  GregorianCalendar calendar = new GregorianCalendar();
  calendar.setTime(someOddestDate);
  GregorianCalendar calendarInput = new GregorianCalendar();
  calendarInput.setTime(inputDate);
  Date reducedDate = null;
try {
    SimpleDateFormat formatter = new SimpleDateFormat(formatPattern, locale);
    formatter.setTimeZone(timezone);
    reducedDate = formatter.parse(formatter.format(someOddestDate));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  GregorianCalendar reducedCalendar = new GregorianCalendar();
  reducedCalendar.setTime(reducedDate);
  int maxField = 0;
  int i = 14;
//    System.out.println("Reduced date is "+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN).format(reducedDate)+" - Oddest date is "+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN).format(someOddestDate));
  while(i > 0 &&  maxField == 0){
//        System.out.println("Reduced Field "+i+" is set "+reducedCalendar.isSet(i)+" and has value "+reducedCalendar.get(i)+" with actual maximum "+   reducedCalendar.getActualMaximum(i)+" and minimum "+reducedCalendar.getActualMinimum(i)+"-> "+reducedCalendar.getDisplayName(i, DateFormat.FULL, Locale.UK));
//        System.out.println("Oddest date Field "+i+" is set "+calendar.isSet(i)+" and has value "+calendar.get(i)+" with actual maximum "+ calendar.getActualMaximum(i)+" and minimum "+calendar.getActualMinimum(i)+"-> "+calendar.getDisplayName(i, DateFormat.FULL, Locale.UK));
      if(reducedCalendar.get(i) == calendar.get(i)){
//            System.out.println("-------> Field "+i+" is equal.");
          maxField = i;
      }
      i--;
  }
  long valueInMillis = Long.MIN_VALUE;
  switch(maxField){
    case 1: valueInMillis = calendarInput.getActualMaximum(6) * 24L * 60 * 60 * 1000; break;// current year granularity
    case 2: valueInMillis = calendarInput.getActualMaximum(5) * 24L * 60 * 60 * 1000; break;// current month granularity
    case 3: //week in month // we just want to know that the granularity is week here and don't care about partial weeks
    case 4: valueInMillis = 7 * 24L * 60 * 60 * 1000; break; // week in year
    case 5: //day granularity
    case 6:
    case 7:
    case 8: valueInMillis = 24L * 60 * 60 * 1000; break; 
    case 9: valueInMillis = 12L * 60 * 60 * 1000; break; //half a day
    case 10: //hour
    case 11: valueInMillis = 60 * 60 * 1000; break; 
    case 12: valueInMillis = 60 * 1000; break; //minute
    case 13: valueInMillis = 1000; break; //second
    case 14: valueInMillis = 1; break; //millisecond
    default: System.err.println("This should never happen.");
  }
//    System.out.println("Returning "+valueInMillis);
return valueInMillis;

它基本上通过将日历集的日历字段与使用所有字段 (oddestDate) 的日期进行比较,并将日历设置为相同的日期,但通过 formatPattern 再次格式化、打印和解析。为了至少补偿闰年,还需要输入日期。

于 2011-09-09T15:56:06.987 回答