Solved

Return formatted time

Posted on 2012-04-01
9
537 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 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

20 Experts available now in Live!

Get 1:1 Help Now