我需要黑莓中的 HTTP 日期格式。例子:
2013 年 4 月 13 日星期六 06:06:13 GMT
我已经实现了。这是我的 Date Util 类代码:
package mypackage;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import net.rim.device.api.i18n.Locale;
import net.rim.device.api.i18n.SimpleDateFormat;
public class DateUtils {
private static SimpleDateFormat _df;
public static String getDate()
{
Calendar cal = Calendar.getInstance();
cal.setTime( new Date() );
int year = cal.get( Calendar.YEAR );
int month = cal.get( Calendar.MONTH ) + 1;
int day = cal.get( Calendar.DAY_OF_MONTH );
return "" + year
+ "-" + month
+ "-" + day;
}
public static String getHttpDate()
{
return getHttpDate( new Date() );
}
public static String getHttpDate( long timestamp )
{
return getHttpDate( new Date( timestamp ) );
}
public static String getHttpDate( Date date )
{
return _df.format( date );
}
public static String getTime()
{
Calendar cal = Calendar.getInstance();
cal.setTime( new Date() );
int hours = cal.get( Calendar.HOUR_OF_DAY );
// use 24 hour clock
int minutes = cal.get( Calendar.MINUTE );
int seconds = cal.get( Calendar.SECOND );
int milli = cal.get( Calendar.MILLISECOND );
return formatTime( hours, minutes, seconds, milli );
}
public static String getTimestamp()
{
Calendar cal = Calendar.getInstance();
cal.setTime( new Date() );
int year = cal.get( Calendar.YEAR );
int month = cal.get( Calendar.MONTH ) + 1;
int day = cal.get( Calendar.DAY_OF_MONTH );
int hours = cal.get( Calendar.HOUR_OF_DAY );
// use 24 hour clock
int minutes = cal.get( Calendar.MINUTE );
int seconds = cal.get( Calendar.SECOND );
int milli = cal.get( Calendar.MILLISECOND );
return "" + year
+ "-" + month
+ "-" + day
+ "_" + formatTime( hours, minutes, seconds, milli );
}
private static String formatTime( int hours, int minutes, int seconds, int milli )
{
StringBuffer buf = new StringBuffer();
buf.append( "" + hours );
buf.append( "." );
if( minutes < 10 )
buf.append( "0" + minutes );
else
buf.append( "" + minutes );
buf.append( "." );
if( seconds < 10 )
buf.append( "0" + seconds );
else
buf.append( "" + seconds );
buf.append( "-" );
if( milli < 10 )
buf.append( "00" + milli );
else if( milli < 100 )
buf.append( "0" + milli );
else
buf.append( "" + milli );
return buf.toString();
}
static
{
_df = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss zzz");
}
}
我正在使用这个类:
private String getDate()
{
String date= DateUtils.getHttpDate(new Date());
if(date.trim().length()!=("EEE, dd MMM yyyy HH:mm:ss zzz").length())
date = date.substring(0, ("EEE, dd MMM yyyy HH:mm:ss zzz").length());
System.out.println("Date is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+date);
return date;
}
我面临的问题是,它在 OS 5 和 Os 7 的模拟器上工作。但是,不在 OS 6 模拟器上。在火炬 9800 模拟器上,我得到
2013 年 4 月 13 日星期六 11:33:19
什么时候,我检查了设备,面临同样的问题。请帮忙,我急需这个。