Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Return formatted time

Posted on 2012-04-01
9
Medium Priority
?
547 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

876 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