-->

黑莓日期时间字符串格式化(Blackberry date time string formattin

2019-06-24 19:11发布

我需要解析的日期/时间字符串,添加原始偏移,并根据当地时区转换。

日期时间,我从服务器获取的格式如下:“2012年5月25日下午2时30分”

HttpDateParser.parse(时间)的方法:该格式不被解析

但HttpDateParser做解析格式为:“25-MAY-2012下午2时30分”

你能告诉我,我应该怎么分析“2012年5月25日下午2时三十”字符串BB获得长期价值。

我有1个更多的问题,我需要显示格式GMT,IST时区,但所有我可以显示是亚洲/加尔各答,美洲/洛杉矶等..

Answer 1:

黑莓Java的缺失,开发商用于在桌面Java,甚至Android的Java应用程序的一些有用的字符串处理和日期格式的API。 所以,你必须做一些解析自己的,这是一个痛苦,如果你需要处理的时区。 你没有说你的服务器是在哪个时区,所以我必须假设你知道,即使你在你的问题显示字符串没有时区信息。

免责声明: 此代码不会马上从我的应用程序之一,因此它并没有经过严格的测试已经。 这是一个概念证明。 你必须测试所有你自己的时区,并可能验证它在夏令时换工作。 此外,看到黑莓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;

然后,代码解析服务器提供的字符串。 我认为这一次无论是在服务器的当前时区,调整为夏令时,或者可能不移动客户端的时区的一些其他已知的时区。 更改我的价值,New_York,对于已知的服务器时区,以满足您的需求:

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

然后,我格式化解析Date的客户端,使用他们的设备的时区,你想对时区显示短代码:

   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()方法仅仅是一个工具来初始化你不喜欢长黑莓时区字符串(“亚洲/加尔各答”)和短代码(“IST”)之间的映射。 据我所知,黑莓没有提供API来显示,人们习惯了,至少对于OS 5.0,其中有许多开发商仍然需要支持的短代码。 您可以选择重构为一个静态实用工具类此。 下面是我用的基础上,映射这篇文章桌面Java 。

我只是懒得填写几个时区字符串的映射,测试了这一点。 如果你想利用这一点,你需要填写休息,或至少与你的某个应用程序的需求, 或者它可能会崩溃 。 请注意,我用的是表需要两个(子)串针对每个时区,夏令时。 如果你喜欢,你可以当你完成工作的更新解决方案。 但是,我会留给你。

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


文章来源: Blackberry date time string formatting