Solved

Return formatted time

Posted on 2012-04-01
9
538 Views
Last Modified: 2012-04-10
Im trying to get the time a button is pressed and display it on the
screen


case R.id.btoMovies:
                       statuschange.setText("To Movies");
                       mystatus="2";
                       Time now = new Time();
                       now.setToNow();
                       timeofchange.setText("" + now);

Open in new window

and it returns 20120401T232303GMT(0,91,0,0,1333322583)

Im trying to get the time formatted  YYYYMMDDTHHMMSS format  ... is
this the best way to do this ?
0
Comment
Question by:dolamitejenkins
  • 6
  • 3
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37794459
I think SImpleDateFormat will work with Androiud as elsewhere in java

http://developer.android.com/reference/java/text/SimpleDateFormat.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794464
So

I think somethidk like that should work:

SimpleDateFormat sdf = new SimpleDateFormat(" "yyyyMMddHHmmssSSSZ":

java.util.Date dd = new java.util.Date();

.setText(sdf.format(dd));
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794475
This is the test:
SimpleDateFormat sdf0401 = new SimpleDateFormat("yyyyMMddHHmmss");

java.util.Date dd0401 = new java.util.Date();


        System.out.println(sdf0401.format(dd0401));

Open in new window


Output:

20120401204808

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794478
If you want to have "T" in between, then you probably want to do it this way:

SimpleDateFormat sdf0401 = new SimpleDateFormat("yyyyMMdd");

        SimpleDateFormat sdf0402 = new SimpleDateFormat("HHmmss");

java.util.Date dd0401 = new java.util.Date();


        System.out.println(sdf0401.format(dd0401) +"T"+sdf0402.format(dd0401));

Open in new window



20120401T205121

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dolamitejenkins
ID: 37794510
its keeps crashing android

04-02 04:04:48.368: E/AndroidRuntime(306):       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

04-02 04:04:48.368: E/AndroidRuntime(306):       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

I cant really see the error because eclipse is tough to debug in /
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37794561
I tried using SimpleDateFormat and java.util.Date
in the HelloAndroid project and it worked fine in the emulator


package com.exmple.helloandroid;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.widget.TableLayout;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = new TextView(this);
        
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     
     java.util.Date dd = new java.util.Date();
     
              
        tv.setText("Hello, absolutely new Android,  " + sdf.format(dd));
        setContentView(tv);
        

       // setContentView(R.layout.main);
    }
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37794574
androis with time
0
 

Author Comment

by:dolamitejenkins
ID: 37794575
Thank you that worked ....
0
 

Author Closing Comment

by:dolamitejenkins
ID: 37830763
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
eclipse argument 14 53
object oriented programming comparison 5 57
A short article about a problem I had getting the GPS LocationListener working.
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now