Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 14
Registered: ‎03-29-2013
My Device: 9800
My Carrier: Vodafone

Need datetime in Sat, 13 Apr 2013 06:06:13 GMT format

hello all, 

I need datetime in the above given format.
I dont how to achieve this using simple date format.
I need to send this in Http Heder while calling a web service.
Please suggest something.

Thanks

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need datetime in Sat, 13 Apr 2013 06:06:13 GMT format

Can you give us an example of what you have tried and why it is failing?

 

Looking at the documentation for SimpleDateFormat what you want looks achieveable so I would like to understand what is causing you a problem. 

Contributor
Posts: 14
Registered: ‎03-29-2013
My Device: 9800
My Carrier: Vodafone

Re: Need datetime in Sat, 13 Apr 2013 06:06:13 GMT format

This is my class code:

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

}
}

and I am using this:


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


Problem is getting Sat, 13 Apr 2013 11:33:19 Asi while i need
Sat, 13 Apr 2013 06:06:13 GMT
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need datetime in Sat, 13 Apr 2013 06:06:13 GMT format

Sorry I am struggling to sort out what your code is doing.  There seems to be loads of code in here that is not relevant to your problem.  Can you give us a small self contained snippet that demonstrates the problem - 3 or 4 lines should be able to do it.

 

But looking at your output, it seems you have the formatting all OK, the issue appears to be the zzz.  Is that correct?  Have you tried various lengths of 'z'?  What is the local time zone?  What is the Time Zone you want to have in the time you send?