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 7710057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:01:08+00:00 2026-06-01T01:01:08+00:00

Friends, I was intended to know how to implement the date and time functions

  • 0

Friends,

I was intended to know how to implement the date and time functions for android and java.

I’m developing an application which shows the posted date of images. I’m showing that date in a list view. For example. I’m showing something like // posted on : 20/3/2012. Instead of it, i want to calculate the time interval betweeen two date and the date i have in type string. Besides, i want to calculate seconds, minutes, hours too. How can i do this..?

Regards,
VELU

Blockquote

After some googling i have got this way to do so. Finally it works.

Blockquote

           SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date endDate = null;
    try {
        endDate = formatter.parse(tochangeDate);
    } catch (ParseException e) {
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = null;

    String curentDateandTime = sdf.format(new Date());
    try {
        startDate = formatter.parse(curentDateandTime);
    } catch (ParseException e) {
    }

    long diff = (startDate.getTime()) - (endDate.getTime());

    int numOfDays = (int) (diff / (1000 * 60 * 60 * 24));
    int hours = (int) (diff / (1000 * 60 * 60));
    int minutes = (int) (diff / (1000 * 60));
    int seconds = (int) (diff / (1000));

This is what i have done and it works charm

  • 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-06-01T01:01:09+00:00Added an answer on June 1, 2026 at 1:01 am

    I have written this quickly in a long winded way because I already had the library procedures to hand.

    package com.me.mytime;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    
    public class MyTimeActivity extends Activity
    {
        private static final String TAG = "MyTimeActivity";
    
        //Activity
        private Context context;
    
        /**
         * Called when the activity is first created
         */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            this.context = this;
    
            Calendar c1 = stringToCalendar("2012-03-20");
            Calendar c2 = stringToCalendar("2012-03-26");
            String elapsedDaysText = getElapsedDaysText(c1, c2);
        }
    
        public static Calendar stringToCalendar(String dateString)
        {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            try
            {
                date = formatter.parse(dateString);
                cal.setTime(date);
            }
            catch(ParseException e)
            {
                cal = null;
            }
            return cal;
        }   
    
        public String getElapsedDaysText(Calendar c1, Calendar c2)
        {
            long milliSeconds1 = c1.getTimeInMillis();
            long milliSeconds2 = c2.getTimeInMillis();
            long periodSeconds = (milliSeconds2 - milliSeconds1) / 1000;
            long elapsedDays = periodSeconds / 60 / 60 / 24;
            String elapsedDaysText = String.format("%d days ago", elapsedDays);
            return elapsedDaysText;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, I like to know using which version of Android SDK we can develop
I am developing an application and testing it on my device running Android 2.2.
Friends, I am developing third party application on Spotify. I am using Spotify Client
Friends, I have an application, ten pages, but full of buttons(with background images), big
Friends, I have a question here. I'm developing an VS2010 - MVC 3 Application
Friends, I know how to deploy and retrieve a single element in LINQ, but
Friends, If I want to know about the new features of Oracle 11gR2 Database
Friends please help. I know that using jdk1.7 we can get the last access
Friends, i m developing a Game. All the drawings are handled by my sSurfaceViewClass,
friends, i am opening google map application by using intents and URL to google

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.