Get time in comment or post in android [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to get time in any post to show how much time has been gone doing this post.
For example in facebook you see the time which is passed away after your post.

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Date past = format.parse("20/08/2015 18:10:52");
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + " seconds ago");
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");

I'm not really sure about what you mean, giving more details would probably help.
If you want to have something like "Posted 10minutes ago", I guess you'd have to store the date/hour of the post. Then when displaying it, doing a simple operation : Time = CurrentTime - PostTime
Let's say you post something at 12:00.
You store the post with the hour
When displaying (at 12:10 for example), you substract the posted hour from the actual hour, and tchiiiing ! "Posted 10 mins ago"
You'll also have to get some work to display a difference between seconds, minutes, hours and day, but basically that's how it would work !

Related

I want to swap an image in imageview with respect to system time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am developing a schedule application. I want to change image 1 to image 2 when in specific time. And is there any method to get the day of week from system.
For the second part of your question how to get the day of week from system try calendar.get() method but remember calendar.get() would return an integer based on zero index: sunday = 0 and saturday =6 and rest of the days lie in between 0 to 6.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.get(Calendar.DAY_OF_WEEK);

How to get my device date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get my android device date and check with current date and compare number of days left. Tab or device doesn't have SIM also.
Thanks!
You can use this code to get the device date.
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
String dateFormat = s.format(new Date(System.currentTimeMillis()));
Android API for managing times android.text.format.Time
For getting current time:
Time time = new Time();
time.setToNow();
To calculate differences between two time, you can try this code
Time time1;
Time time2;
long diff = time1.toMillis(false) - time2.toMillis(false);
// Now 'diff' contains difference between those two time in milliseconds
Update #1
This class was deprecated in API level 22.

How to start the current time when the installation is done [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to start the time when the installation is done, I also want to calculate and display the number of remaining days for the expiry date by day.
How to use this code at the time of the installation, if any body very familour with this please help me out.
I simply want to implement a trial period for my app, and also for each twenty four hours, I want to show to the user the remaining days for the expire of the app. I dont want to worry about the Uninstallation and again installing the app.
You will have to store date value in shared preference when your app opens for the first time
then follow the below code
Date StoredDate = new Date(prefs.getLong("time", CurrentDate.getTime())); // store this on first installation
Date CurrentDate = new Date(System.currentTimeMillis());// check every time when app opens
if(daysBetween(StoredDate,CurrentDate)>=10) //** check the condition for number of days *//*
{
condition_Install_valid(MainActivity.this);
Toast.makeText(getApplicationContext(), "Version Expired", Toast.LENGTH_SHORT).show();
SharedPreferences.Editor editor = prefs.edit();
editor.commit();
}
/** Code to check the date object and give days */
public static int daysBetween(Date d1, Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
//check first run
boolean firstrun = getSharedPreference("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
if (firstrun){
[[ ...Display the dialog ]]
// Save the state
getSharedPreference("PREFERENCE", MODE_PRIVATE)
.edit()
editor.putLong("time", CurrentDate.getTime());
.putBoolean("firstrun", false)
.commit();
}
}

How to get all days of week using week number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am confused a lot, by this thing. Actually I have developed one view pager, which is showing data week wise. I mean first screen is for current week of the year, and its related dates. Then now I swipe the screen I want next number of weeks and its dates.
i.e. For now, if current date is 2014/01/16 then current week number is 03. But now when I swipe the screen, I want 04th week dates of January.
Thanks in advance.
void getStartEndOFWeek(int enterWeek, int enterYear){
//enterWeek is week number
//enterYear is year
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, enterWeek);
calendar.set(Calendar.YEAR, enterYear);
SimpleDateFormat formatter = new SimpleDateFormat("ddMMM yyyy"); // PST`
Date startDate = calendar.getTime();
String startDateInStr = formatter.format(startDate);
System.out.println("...date..."+startDateInStr);
calendar.add(Calendar.DATE, 6);
Date enddate = calendar.getTime();
String endDaString = formatter.format(enddate);
System.out.println("...date..."+endDaString);
}
and also reverese
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2013);
now.set(Calendar.MONTH,04);//0- january ..4-May
now.set(Calendar.DATE, 04);
System.out.println("Current week of month is : " +
now.get(Calendar.WEEK_OF_MONTH));
System.out.println("Current week of year is : " +
now.get(Calendar.WEEK_OF_YEAR));

Converting string "ex: PT20H" to time in android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm developing an android application.
This time format returns to me as a string from the database -->"PT20H" and I want to convert it to the following format --> "20:00".
Any ideas??
Thanks in advance.
What you want to do is called parsing a time string. Take a look at SimpleDateFormat
Its seems like you've got some plain text that isn't informative, being the PT and the H. That is, if all your times are whole hours only (eg "PT20H23M" for 20:23 does not occur. I believe the pattern to use should be "'PT'H'H'"
That is PT (between quotes to signify text to ignore, then the hour (symbol H) and then H between quotes again because it is to be ignored.
SimpleDateFormat sdf = new SimpleDateFormat("'PT'H'H'");
Date theDate = sdf.parse("PT20H");
SimpleDateFormat newFormat = new SimpleDateFormat("H:m");
String newFormattedString = newFormat.format(theDate);

Categories

Resources