Android Current Time [closed] - android

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.

Related

String to Date in Android [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 7 years ago.
Improve this question
i have a string which means a date. it looks like this :
/Date(1448880305230+0200)/
So how to convert it to Calendar or any type of date ?
The string above in your question doesn't look like unixtime, but technically speaking you can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// *1000 is to convert seconds to milliseconds
Date date = new Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
String formattedDate = sdf.format(date);
//Log it in
//System.out.println(formattedDate);
//Log.d(formattedDate);

Compare current date with stored one

For an easteregg in my Android app, I have to compare the current date with a stored date - and I only need to know if it's the right month.
I know that System.currentTimeMillis() is the fastest way to get the current time but now I need to get the current month from that. I avoided String comparison for it's known flaws.
My awful implementation works but it really doesn't look correct and efficient:
if (Integer.parseInt((String) DateFormat.format("MM",System.currentTimeMillis()))==12) //xmas "easteregg"
xmasBool=true;
Is there any more elegant solution for this?
Here's a better solution:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // Date's default constructor gives you current time
xmasBool = calendar.get(Calendar.MONTH) == Calendar.DECEMBER;
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
And now you can compare the variable month with your stored month.
You can compare a day or a month or both, the whole date by formatting java.Util.Date using SimpleDateFormat.
Eg.
new SimpleDateFormat("dd").format(new java.util.Date())
gives you the "day" value. Similarly "MM" will give you the month. Use combination of those as per your requirement.
Store it in the same format and you have a common standard for comparison.

Get days until date based on timezone on phone

I´ve been trying to figure this out for a while but can not wrap my head around it.
I´m working on an android app and i want to display left to a specific date, and i want the number of days based on what time zone i have set on my phone.
I have Joda Time in my app and the information i have is for example:
2013-05-05 9.00PM the time is in PST (GMT-8) timezone, i have no clue how to do this and i have searched both on google and SO but can not get a clear answer.
EDIT
I managed to solve my problem with code found here on SO
String dateString = airDate + " " + airTime.toUpperCase();
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd K:mma");
sourceFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));
Date parsed;
parsed = sourceFormat.parse(dateString);
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd");
destFormat.setTimeZone(tz);
result = destFormat.format(parsed);
However I found out that the times i first got isn´t correct and I now get the time zone in the format GMT-5 +DST. And I dont´t know what to do with the +DST if setting the time to 20:00 and using GMT-5 in my TimeZone.getTimeZone the time returned is 22:00 which is "wrong" since I live in sweden. I would appreciate any help with this.
If you could settle for a string that looks like "in 2 days", then you should be able to use DateUtils.getRelativeTimeSpanString().

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

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);

System.currentTimeMillis() in android?

i face problem with System.currentTimeMillis() in my project i write some code here where i got problem
Date currentDate = new Date(System.currentTimeMillis());
Log.v("1st",""+currentDate);
Date currentDate = new Date(System.currentTimeMillis()+25*24*60*60*1000);
Log.v("2nd","25th"+currentDate);
it displays current date see in first log but i add 25 days to current date it is in 2nd log but it is not working it displays 2 months back day. it is working very fine in between 1*24*60*60*1000 to 24*24*60*60*1000 days.after 24 it is not working please solve my problem
thanks in advance
25*24*60*60*1000>Integer.MAX_VALUE,
your should write as below:
new Date(System.currentTimeMillis()+25*24*60*60*1000l);
use Calendar instead
Calendar rightNow = Calendar.getInstance()
rightNow.add(Calendar.DAY_OF_YEAR, 25)
and the you can get the date object
You are mixing ints and longs. My java is a little rusty, but try:
Date currentDate = new Date(System.currentTimeMillis()+25L*24L*60L*60L*1000L);

Categories

Resources