I've being using System.currentTimeMillis() to get the current time of an action occuring in my Android activity. The time it gives when converted to a date something like this Thu 20 January 1970 20:15. I am wondering if this is just because I am using it on the emulator of is there something gone wrong?
EDIT:
times = System.currentTimeMillis();
//Converting to Date using constructor Date(long milliseconds)
Date changeToDate = new Date(times);
It's just a guess: you probably convert millis to date incorrectly. Don't you divide them by 1000 while passing to Date constructor?
which Date class did you import in your project? probably you have imported java.sql.Date but the one which you should use is java.util.Date - perhaps this helps a little bit.. #a.ch. : java.util.Date needs milliseconds, not seconds (ms/1000) in its constructor ;)
Related
I have a month July 2022 for example, I want get epoch milis for the first day of the month
1st July 2022 at midnight.
from the month I was able to get the 1st July 2022, but how to convert it into epoch milis for 1st July 22 midnight
val datey = "July/2020"
val dateFormaty = DateTimeFormatter.ofPattern("MMMM/yyyy")
val yearMonthy = YearMonth.parse(datey, dateFormaty)
val parsedDatey = yearMonthy.atDay(1)
I get 2022-07-01 for parsedDate, I want to get the date time for this date in epoch milis
Thanks
R
Like I mentioned, LocalDate does not actually store any time information whatsoever, so transforming it to epoch isn't possible. Technically. Yet it is with some possible inacuracies.
How about something like this:
make the following extension function
fun LocalDate.toDate(): Date = Date.from(this.atStartOfDay(ZoneId.systemDefault()).toInstant())
Note that it uses the system default timezone for this, to provide the necessary information.
then just use it.
val myDate = myLocalDate.toDate()
which would in your case, be parsedDatey.toDate()
But, we don't really even need the Date here. Lets avoid casting the LocalDate to Date then getting the epoch milli from there, and just do it from the provided Instant instead.
So the real answer to your question is this:
fun LocalDate.getEpochMillis(): long = this.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
// call this in your parsing method you posted
parsedDatey.getEpochMillis()
I am having following code to convert milliseconds to Android Date object.
Date dateObj = new Date(milli);
But problem is that my milliseconds value is having GMT value added in it before i pass it to Date class, add when i print this date object i can see that date object is again adding GMT value in the milliseconds value and because of that my date is displayed as wrong.
So how can i generate Date object with out considering GMT value in it.
For example my milliseconds are 1385569800000 which is getting printed as below:
Wed, 27 Nov 2013 22:00:00 --> +5.30
But the current value of this time stamp without adding GMT is:
Wed, 27 Nov 2013 16:30:00
*UPDAE*
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
So basically i want to create Date object which has proper date time value in it with out adding extra GMT time added in it.
A Date is always in UTC. No need to change that.
When printing the date value, use SimpleDateFormat and call setTimeZone() on it before formatting the output string.
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
TimerTask is just a task and not its scheduling. Timer accepts a Date object for scheduling. The Date is in UTC there as well.
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
You can try with joda-time API.
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
http://joda-time.sourceforge.net/key_instant.html
A Date object simply represents a moment in time. Imagine you're on the phone to someone on a different continent, and you say "3...2...1...NOW!". That "NOW" is the same moment for both of you, even though for one person it's 9am and for the other it's 4pm.
You're creating a Date representing the moment 1385569800000 milliseconds after the Java epoch (the beginning of 1970, GMT). That is your "NOW", and it's fixed and unchanging. What it looks like converted into text, however, depends on which timezone you want to display it for. Java defaults to using GMT, which would be right if you were in Britain during the winter, but for (I'm guessing) India you want it in a different time zone. Laalto's answer shows you how to do that.
here is the code,that worked like charm for me:
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
Here is my code to assign a value to the Date:
long saveddatevalue = new Date().getTime();
I just want to know, what does this saved long variable actually show? I wish to use this to output values within a certain number of days.
Thanks!
It will basically give you the same result as System.currentTimeMillis() - the number of milliseconds since the Unix epoch of midnight January 1st 1970, UTC.
It is far easier to work with java.util.Calendar.add(int, int) for manipulating dates. Here is some sample code.
This has probably been asked and answered a million times, but I can't seem to find a solution anywhere. Upon starting an activity in an android app, I want to display the current date and time. From what I understand the date part can be done simply with the following:
Date d = new Date();
d.getTime();
CharSequence s = DateFormat.format("EEEE, MMMM d, yyyy", d.getTime());
TextView date = (TextView)findViewById(R.id.dateText);
date.setText(s);
TextView time = (TextView)findViewById(R.id.timeText);
time.setText(s);
In eclipse it gives me an error and says that the constructor date is undefined. I chose the auto fix option and it added a 0 as a parameter in the Date constructor. This produced a date, but the date is Dec. 31, 1969. What am I missing here?
This is probably trivial, but I'm still new to this stuff.
Thanks in advance for any advice.
You are probably using java.sql.Date. You want to be using java.util.Date.
From http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html for constructor Date(long time):
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
Instead, take a look at http://developer.android.com/reference/android/os/SystemClock.html
Import java.util.Date and your problem will be resolved.
I know there is the Date() class built into the API, but what line of code actually grabs the time of day?
use this Calendar.get(Calendar.HOUR_OF_DAY) take a look at link text
The getTime() method returns the number of milliseconds since 1970. However, it's a long and will not fit into an integer.
Date now = new Date();
long millis = now.getTime();
See System in Android docs for proper uses, but you could
import java.lang.System;
long now = System.getcurrentTimeMillis()