How to calculate the total number of days passed [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate date/time difference in java
I am providing the user with the option to select the date using Date Picker. Is there any in-built method using which I can calculate the duration in days wrt to user selected date and todays date.

I don't like answering this because there were millions of questions like this (use search option before posting questions). Use Joda Time. There is a Period class, which will be useful for you.

Get the difference between the two times in milliseconds. Than you can get the Days via Java's Calendar class.

Date today = new Date(); // the date of today
Date target = new Date(); // the date of when the user picks
long todayEpoch = today.getTime(); // or can use = System.currentTimeMillis();
long targetEpoch = target.getTime();
long daysInMs = targetEpoch - todayEpoch; //days in MS's
//the # of days
float days = (daysInMs/1000/60/60/12);

Related

How to convert a timestamp to years, month, days and hours in Android?

I want to convert a timestamp like 62207486144 to days(like 1 year 6 months 2 days 3 hours 33 minutes) in my Android App. How can I do that? I am able to get days and hours but not years or months with the following code-
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(62207486144);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(calendar.getTime());
long timestamp = 62207486144;
long days = TimeUnit.MILLISECONDS.toDays(timestamp );
timestamp -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(timestamp );
Years- divide days by 365 (or 365.25 if you want to account for leap years). Months- well, months aren't exact because months aren't the same length, but dividing by 30 is going to be about right.
Your code above is a bit odd though. The first 4 lines are doing something totally different than the last 4. The first 4 would get you data about a specific time in a timestamp- you'd use that if you wanted to figure out for a timestamp what day/month/year it was. The last 4 treat it as a duration. You'd use that for figuring out how long something took. My suggestion above works for durations. If you want to know when a particular timestamp was instead, you'd just use the calendar object to tell you that.
check this out, as an easy way to convert to localDateTime.
From there, it should be way easier.
long millis = 62207486144L;
LocalDateTime date = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).toLocalDateTime();
date.getDayOfMonth(); //Day
date.getMonthValue(); //Month
date.getYear(); //Year
More information here: https://howtoprogram.xyz/2017/02/11/convert-milliseconds-localdatetime-java/

Android Current Time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to show current time of device in app. following is the code is getting time but problem is if current time is 12:15 but my variable cTime has opposite time that is 00:15. Below is my code for getting time, please help me out
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar c = Calendar.getInstance();
int currentHour = c.get(Calendar.HOUR);
int currentMin = c.get(Calendar.MINUTE);
String currentTime = currentHour+":"+currentMin;
Date cTime = sdf.parse(currentTime);
You should use Calendar.HOUR_OF_DAY instead of Calendar.HOUR to get the hour in 0-24 format.
Try this Code For current time
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa");
String datetime = sdf.format(now);
Use this simple method for get device current time in milisecound
System.currentTimeMillis();
Always, always read the documentation for the code you're using.
You are formatting your hours with the H pattern, which describes the 24 hour model:
H | Hour in day | (0-23) | Number | 0
You need to use the small h, which represents the “Hour in am/pm (1-12)”:
SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date = new Date();
String dateReadable = format.format(date);
// Use your String elsewhere.
And this is all you need to get started. No need to create ints and Calendars for this. You can also put the a for the period indicator, as Arjun said below.
This is the foundation for this answer, too, but considering the code is slightly different and you are facing difficulties (by looking at your code), I didn't consider it a duplicate.

Get Week from given Calendar Range?

I am implementing an application where I have two calendar object with minimum and maximum date range.
Is there a way to get total week count
How can get week information based on index say: need 5th week of calendar range
any one have suggestion here ?
As mentioned here Get the number of weeks between two Dates. maybe you can use JodaTime http://www.joda.org/joda-time/
android version https://github.com/dlew/joda-time-android
DateTime dateTime1 = new DateTime(date1);
DateTime dateTime2 = new DateTime(date2);
int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();

How to find difference two dates on android app [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate date/time difference in java
I am a new on Android and I want to make a new app for me. I want to subtruct two dates (dd/mm/yyyy) format by using currentDate. For example I want to find the differences of the days between 01/02/2013 and currentDate. How can I do it?
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d = null;
java.util.Date d1 = null;
Calendar cal = Calendar.getInstance();
try {
d = dfDate.parse("01/02/2012 ");
d1 = dfDate.parse(dfDate.format(cal.getTime()));//Returns 15/10/2012
} catch (java.text.ParseException e) {
e.printStackTrace();
}
int diffInDays = (int) ((d.getTime() - d1.getTime())/ (1000 * 60 * 60 * 24));
System.out.println(diffInDays);
Check Ex1 and Ex2 for more detailed example
You may take a look into DateUtils, that offers some utility methods to compute elapsed time between two dates, but only for representation. Do not use the direct approach to convert between days/milliseconds since it does not take into consideration leap years, leap seconds, summer/winter time changes, etc. Those considerations are handled by Calendar classes.
Update
You can use Joda library to perform relative date and time calculations like the one you need.

how do I get the UTC milliseconds for today at 6:15PM? [duplicate]

This question already has answers here:
GPS Time Representation library
(4 answers)
Closed 9 years ago.
How do I get the UTC milliseconds for today at a given time, say 6:15PM?
I want that as a start time then I will use the GPS getTime() function and be able to compute how much time until my start as the race committee always uses GPS time.
Looking at this documentation Calendar, I would think I would just need to call Calendar(); then get(MONTH), get(YEAR), etc and do a set(year, month, day, 18, 15,0); but that doesn't work.
Maybe it is Calendar.YEAR, etc but I have not figured out the set equivalent and I get warnings.
I would appreciate some help.
// get calendar instance, substitute timezone for which ever you need or leave blank to use current
Calendar updateTime = Calendar.getInstance(TimeZone.getTimeZone("US/Pacific"));
updateTime.set(Calendar.HOUR_OF_DAY, 18); // set hour
updateTime.set(Calendar.MINUTE, 15); // set minute
updateTime.set(Calendar.SECOND, 0); // set seconds
long time = updateTime.getTimeInMillis(); // get milliseconds

Categories

Resources