Pergunta

I am Stuck on something. What I want is to get system date and time, Store it in string ,then add random time in other strings , then compare them with each other using if else ( eg

if(dateTime1 > dateTime2) {

}

Then I want to show the message like displayed in gmail ( If the mail is send today gmail shows the time today at 5 pm ). And I want to show those all in text Boxes . I want the message like today 4pm , yesterday 4 pm and if it exceeded a week then simply show the date and time. Need your help hope you got what I want. Kindly help me.This is what I done so far. Also tried many other things.

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView ShowDate = (TextView) findViewById(R.id.ShowDate);
    TextView ShowToday = (TextView) findViewById(R.id.ShowToday);
    TextView ShowWeek  = (TextView) findViewById(R.id.ShowWeek );
    TextView ShowAgo  = (TextView) findViewById(R.id.ShowAgo ); 
    Calendar ci = Calendar.getInstance();

    String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" +
        (ci.get(Calendar.MONTH) + 1) + "-" +
        ci.get(Calendar.DAY_OF_MONTH) + " " +
        ci.get(Calendar.HOUR) + ":" +
        ci.get(Calendar.MINUTE) +  ":" +
        ci.get(Calendar.SECOND);
Foi útil?

Solução

ohk i will give u a example which a make in my code i take diff of time which is current and from 1 jan 1970 i calculate time diff like this may this will help u just put your time instead of my time rest of all code as it is use

 Calendar cal = Calendar.getInstance();
    Date currentLocalTime = cal.getTime();
    DateFormat date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");  
    date.setTimeZone(TimeZone.getTimeZone("GMT")); 
    String localTime = date.format(currentLocalTime); 

    long currenttime = Constant.retunlongdate(localTime);
    long fixtimejan = Constant.retunlongdate("01/01/1970 00:00:00 AM");

    long nTimeStamp = (currenttime - fixtimejan)/1000;
    System.out.println("and result is == " + nTimeStamp);

 public static long retunlongdate(String givenDateString)
        {

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); 

        long timeInMilliseconds=0;
        try {
            Date mDate =sdf.parse(givenDateString);
              timeInMilliseconds = mDate.getTime();
            System.out.println("Date in milli :: " + timeInMilliseconds);
            return timeInMilliseconds;
        } catch (ParseException e) {
                    e.printStackTrace();
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
        return timeInMilliseconds;
    }

Outras dicas

This will help you for storing date in string format- SimpleDateFormat

for showing time in textview like gmail you have to check current time with store time,which you will get form simple date format from string to data. For comparing date refer this- Compare date

If you want to get the current date and time of system. You can use this code.

long millis = System.currentTimeMillis();
Date date = new Date(millis);

After getting the date object convert it into string ex: date.toString();

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top