Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:30:54+00:00 2026-05-27T04:30:54+00:00

I am new to Android and trying to practice by making small codes. I

  • 0

I am new to Android and trying to practice by making small codes.
I am trying to get difference in dates and show text views based on it.
But I am gettting different difference on same day on my Android Phone.
like when I open app, it says difference in days is 5.
I close it, re open it, now it says diff is 4 !

On emulator it is working fine.
I am using following code.
Can anyone pls suggest some improvement ?

package com.practice.tablet;



import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TabletsSchedule extends Activity {
    public TextView tvToday;
    public TextView tvMedStartDate;
         public TextView tvMessage;
    public TextView tvDiffInDays;
    public static final String LOGGER = "ADITYA";

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

        tvToday=(TextView)findViewById(R.id.TextView01);
        tvMedStartDate=(TextView)findViewById(R.id.TextView001);
        tvMessage=(TextView)findViewById(R.id.TextView05);
        tvDiffInDays=(TextView)findViewById(R.id.TextView06);

        String[] months = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"};

        Calendar today = Calendar.getInstance();

        int day= today.get(Calendar.DAY_OF_MONTH);
        int month=today.get(Calendar.MONTH);
        int year=today.get(Calendar.YEAR);

        String strMonth = months[month];
        Log.d(LOGGER, "displaying today");

        tvToday.setText("Today is "+day+" "+strMonth+" "+year);

        Calendar medStartDate = Calendar.getInstance(); 
     //DOB.set(year, monthOfYear+1, dayOfMonth);

        medStartDate.set(Calendar.DAY_OF_MONTH, 27);
        medStartDate.set(Calendar.MONTH,10);
        medStartDate.set(Calendar.YEAR,2011);

        int day1= medStartDate.get(Calendar.DAY_OF_MONTH);
        int month1=medStartDate.get(Calendar.MONTH);
        int year1=medStartDate.get(Calendar.YEAR);

       String strMonth1 = months[month1];
        //String med = medStartDate.toString().toString();

       tvMedStartDate.setText("Medicine Start Date is "+day1+" "+strMonth1+" "+year1);

        long diff = today.getTimeInMillis() - medStartDate.getTimeInMillis();

        long days = diff / (24 * 60 * 60 * 1000); 

        tvDiffInDays.setText("Difference in days is "+days);
        Log.d(LOGGER, "days is "+days);

        if (days%2!=0){

                        tvMessage.setText("Today you have to take two tablets");

        }
        if (days%2==0){

                        tvMessage.setText("Today you have to take one tablet");

        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:30:55+00:00Added an answer on May 27, 2026 at 4:30 am

    Use this:

    ...
    Calendar medStartDate = Calendar.getInstance(); 
    
    medStartDate.clear();
    
    medStartDate.set(Calendar.DAY_OF_MONTH, 27);
    medStartDate.set(Calendar.MONTH,10);
    medStartDate.set(Calendar.YEAR,2011);
    
    ...
    

    Also you should store important values of today object somewhere(Ex.day,month,year etc.) and also should clear it likewise. after that,you can put those values back and then finally you can use it to compare with medStartDate.it will avoid the problem you are getting because of tiny different of time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Android platform and trying to establish a http request but
I'm new to android but I've done the notepad tutorial. Now I'm trying to
I'm new to android and I'm trying to figure out how to get the
I am new to android.I am trying to add and remove views as in
I'm extremely new to Android and just trying to get my head around these
Hai am new in android Application,am trying to get the latitude and longtitude using
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I'm new to Android and I think I'm trying to do something really basic:
hey, im new to android development and trying to make my first application. What
I'm all new to Android and I'm trying to create a spinner programmatically and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.