Datetime Blackberry日期时间字符串格式

Datetime Blackberry日期时间字符串格式,datetime,blackberry,java-me,timezone,dst,Datetime,Blackberry,Java Me,Timezone,Dst,我需要解析日期/时间字符串,添加原始偏移量并根据本地时区进行转换 我从服务器获取的日期时间格式为:“05-25-2012 02:30 PM” 此格式不由:HttpDateParser.parse(time)方法解析 但是HttpDateParser确实解析了这种格式:“2012年5月25日02:30下午” 您能告诉我,我应该如何解析BB中的“05-25-2012 02:30 PM”字符串以获得长值吗 我还有一个问题,我需要以GMT、IST格式显示时区,但我只能显示亚洲/加尔各答、美国/洛杉矶等等

我需要解析日期/时间字符串,添加原始偏移量并根据本地时区进行转换

我从服务器获取的日期时间格式为:“05-25-2012 02:30 PM”

此格式不由:HttpDateParser.parse(time)方法解析

但是HttpDateParser确实解析了这种格式:“2012年5月25日02:30下午”

您能告诉我,我应该如何解析BB中的“05-25-2012 02:30 PM”字符串以获得长值吗


我还有一个问题,我需要以GMT、IST格式显示时区,但我只能显示亚洲/加尔各答、美国/洛杉矶等等。

BlackBerry Java缺少一些有用的字符串处理和日期格式化API,开发者在桌面Java,甚至安卓Java应用程序中都会用到这些API。所以,您必须自己进行一些解析,若您需要处理时区,那个将是一件痛苦的事情。您没有说明服务器所在的时区,因此我必须假设您知道,即使您在问题中显示的字符串没有时区信息

免责声明:此代码不是直接从我的某个应用程序中产生的,因此它没有经过严格的测试。这是概念的证明。您必须测试您自己的所有时区,并可能验证它在夏令时转换期间是否有效。另外,有关我在这里使用的
TimeZone.getOffset()
方法的限制信息,请参阅

首先,我的进口:

import java.util.Date;
import java.util.TimeZone;
import java.util.Hashtable;
import java.util.Calendar;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.io.http.HttpDateParser;
import net.rim.device.api.util.StringUtilities;
然后,代码将解析服务器给定的字符串。我假设这段时间要么在服务器的当前时区(根据夏令时调整),要么在其他已知时区(可能不是移动客户端的时区)。更改已知服务器时区的我的值,纽约
,以满足您的需要:

   private Date parseServerTime(String timeFromServer) {
      // separate the time string into tokens, which accepts ' ' or '-' or ':' as delimeters
      String tokens[] = StringUtilities.stringToKeywords(timeFromServer);
      int month = Integer.parseInt(tokens[0]);
      int day = Integer.parseInt(tokens[1]);
      int year = Integer.parseInt(tokens[2]);
      int hours = Integer.parseInt(tokens[3]);
      int minutes = Integer.parseInt(tokens[4]);
      boolean isPm = tokens[5].equalsIgnoreCase("PM");
      // convert hours to 24 hour time
      if (hours == 12 && !isPm) {
         hours = 0;
      } else if (hours != 12 && isPm) {
         hours += 12;
      }
      int millisToday = ((hours * 60) + minutes) * 60 * 1000;

      StringBuffer reformattedTime = new StringBuffer();
      // reorder the year, month and day to be able to use HttpDateParser (YYYY-MM-DDTHH:mm)
      reformattedTime.append(tokens[2]).append('-').append(tokens[0]).append('-').append(tokens[1]);
      reformattedTime.append('T').append(hours).append(':').append(minutes);
      long unadjustedTimeFromServer = HttpDateParser.parse(reformattedTime.toString());

      int monthIndex = month - 1;
      // This example assumes the server is giving us a time from the Eastern US time zone
      TimeZone serverZone = TimeZone.getTimeZone("America/New_York");
      int serverOffset = serverZone.getOffset(1, // 1 -> AD, not BC
            year, 
            monthIndex, 
            day, 
            1, // I'm not sure, but I don't think day-of-week matters here?
            millisToday);

      return new Date(unadjustedTimeFromServer - serverOffset);
   }
然后,我使用客户端设备的时区和时区显示所需的短代码为客户端格式化解析的
日期

   private String formatClientTime(Date dateFromServer) {
      TimeZone clientZone = TimeZone.getDefault();
      String tzKey = clientZone.toString();
      String[] tzDisplayNames = StringUtilities.stringToKeywords((String) _timeZones.get(tzKey));

      // we might have to use the "daylight" time zone short code
      Calendar cal = Calendar.getInstance(clientZone);
      cal.setTime(dateFromServer);
      int clientOffset = clientZone.getOffset(1, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK), cal.get(Calendar.MILLISECOND));
      int rawOffset = clientZone.getRawOffset();

      // if the daylight savings time adjusted offset isn't equal to the raw offset, we
      //   must be in daylight savings time
      boolean isTimeDuringDaylightSavings = (clientOffset != rawOffset);
      String tzCode = isTimeDuringDaylightSavings ? tzDisplayNames[1] : tzDisplayNames[0];

      // TODO: change this to whatever output format you want to use for the UI
      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
      return formatter.formatLocal(dateFromServer.getTime()) + " " + tzCode;
   }
我是这样使用这些代码的:

  mapTimeZoneCodes(); 

  Date date = parseServerTime("05-25-2012 02:30 PM");
  String text = formatClientTime(date);
我的
mapTimeZoneCodes()
方法只是一个实用程序,用于初始化您不喜欢的长BlackBerry时区字符串(“Asia/Calcutta”)和短代码(“IST”)之间的映射。据我所知,BlackBerry没有提供API来显示人们习惯的短代码,至少在OS5.0中是这样,许多开发者仍然需要支持它。您可以选择将其重构为静态实用程序类。这是我使用的映射,基于

我只是费心填写了几个时区字符串映射,以测试这一点。如果你想使用它,你需要填写剩下的部分,或者至少是你的应用需要的部分,,否则它可能会崩溃。请注意,对于夏令时,我使用的表对于每个时区需要两个(子)字符串。如果您愿意,可以在完成工作后更新解决方案。但是,我会把这个留给你

   private Hashtable _timeZones;

   private void mapTimeZoneCodes() {
      // TODO: you can uncomment this code and use it to generate your map of time zone ids and short codes
      //String[] ids = TimeZone.getAvailableIDs();
      //for (int i = 0; i < ids.length; i++) {
      //   System.out.println(" _timeZones.put(\"" + ids[i] + "\", \"\");");
      //}
      _timeZones = new Hashtable();
      _timeZones.put("Pacific/Kwajalein", "");
      _timeZones.put("Pacific/Midway", "");
      _timeZones.put("Pacific/Honolulu", "");
      _timeZones.put("America/Anchorage", "");
      _timeZones.put("America/Tijuana", "");
      _timeZones.put("America/Los_Angeles", "PST PDT");
      _timeZones.put("America/Phoenix", "");
      _timeZones.put("America/Denver", "MST MDT");
      _timeZones.put("America/Tegucigalpa", "");
      _timeZones.put("America/Tegucigalpa_2", "");
      _timeZones.put("America/El_Salvador", "");
      _timeZones.put("America/Regina", "");
      _timeZones.put("America/Chicago", "CST CDT");
      _timeZones.put("America/Mexico_City", "");
      _timeZones.put("America/Mexico_City_2", "");
      _timeZones.put("America/Bogota", "");
      _timeZones.put("America/Indianapolis", "");
      _timeZones.put("America/New_York", "EST EDT");
      _timeZones.put("America/Caracas", "");
      _timeZones.put("America/La_Paz", "");
      _timeZones.put("America/Manaus", "");
      _timeZones.put("America/Santiago", "");
      _timeZones.put("America/Halifax", "");
      _timeZones.put("America/St_Johns", "");
      _timeZones.put("America/Montevideo", "");
      _timeZones.put("America/Guyana", "");
      _timeZones.put("America/Buenos_Aires", "");
      _timeZones.put("America/Sao_Paulo", "");
      _timeZones.put("America/Godthab", "");
      _timeZones.put("America/South_Georgia", "");
      _timeZones.put("Atlantic/Cape_Verde", "");
      _timeZones.put("Atlantic/Azores", "");
      _timeZones.put("GMT", "GMT");
      _timeZones.put("Europe/Dublin", "");
      _timeZones.put("Africa/Luanda", "");
      _timeZones.put("Europe/Amsterdam", "");
      _timeZones.put("Europe/Belgrade", "");
      _timeZones.put("Europe/Brussels", "");
      _timeZones.put("Europe/Belgrade Yugoslavia(YU)", "");
      _timeZones.put("Africa/Windhoek", "");
      _timeZones.put("Asia/Amman", "");
      _timeZones.put("Africa/Harare", "");
      _timeZones.put("Asia/Jerusalem", "");
      _timeZones.put("Europe/Minsk", "");
      _timeZones.put("Africa/Cairo", "");
      _timeZones.put("Asia/Beirut", "");
      _timeZones.put("Europe/Athens", "");
      _timeZones.put("Europe/Helsinki", "");
      _timeZones.put("Asia/Kuwait", "");
      _timeZones.put("Africa/Nairobi", "");
      _timeZones.put("Asia/Baghdad", "");
      _timeZones.put("Europe/Moscow", "");
      _timeZones.put("Asia/Tehran", "");
      _timeZones.put("Asia/Tbilisi", "");
      _timeZones.put("Asia/Muscat", "");
      _timeZones.put("Asia/Baku", "");
      _timeZones.put("Asia/Yerevan", "");
      _timeZones.put("Asia/Caucasus", "");
      _timeZones.put("Asia/Kabul", "");
      _timeZones.put("Asia/Karachi", "");
      _timeZones.put("Asia/Tashkent", "");
      _timeZones.put("Asia/Yekaterinburg", "");
      _timeZones.put("Asia/Calcutta", "");
      _timeZones.put("Asia/Colombo", "");
      _timeZones.put("Asia/Katmandu", "");
      _timeZones.put("Asia/Dhaka", "");
      _timeZones.put("Asia/Almaty", "");
      _timeZones.put("Asia/Rangoon", "");
      _timeZones.put("Asia/Bangkok", "");
      _timeZones.put("Asia/Krasnoyarsk", "");
      _timeZones.put("Asia/Hong_Kong", "");
      _timeZones.put("Asia/Kuala_Lumpur", "");
      _timeZones.put("Australia/Perth", "");
      _timeZones.put("Asia/Taipei", "");
      _timeZones.put("Asia/Irkutsk", "");
      _timeZones.put("Asia/Tokyo", "");
      _timeZones.put("Asia/Seoul", "");
      _timeZones.put("Asia/Yakutsk", "");
      _timeZones.put("Australia/Darwin", "");
      _timeZones.put("Australia/Adelaide", "");
      _timeZones.put("Pacific/Guam", "");
      _timeZones.put("Asia/Vladivostok", "");
      _timeZones.put("Australia/Hobart", "");
      _timeZones.put("Australia/Brisbane", "");
      _timeZones.put("Australia/Sydney", "");
      _timeZones.put("Asia/Magadan", "");
      _timeZones.put("Pacific/Fiji", "");
      _timeZones.put("Pacific/Auckland", "");
      _timeZones.put("Pacific/Tongatapu", "");
   }
private Hashtable\u时区;
私有void mapTimeZoneCodes(){
//TODO:您可以取消对该代码的注释,并使用它生成时区ID和短代码的映射
//字符串[]id=TimeZone.getAvailableIDs();
//for(int i=0;i