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);
Related
I want to get the date of SMS in format dd-MM-yyyy and time in format HH:MM (in 24hr format) on which it was received on phone. I use the following code
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(messages[0].getTimestampMillis());
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
String finalDateString = formatter.format(calendar.getTime());
Log.i(TAG, "Date new Format " + finalDateString);
Message was recieved on 11:50PM on 26-Dec-19 at phone, but I get the result in Log "Date new Format 27/12/2019 10:50:03.000". Please notice instead of 26 its giving 27 and time is also 1 hr less, instead of 11 its 10.
Is this a normal behaviour? Do I have to subtract 1 day from date to get correct data and add 1 hr in time? Will this always work correctly? Do I have to specify timezone to get correct data? Please advise as I got confused with ths.
Try as follow
formatter.timeZone = TimeZone.getTimeZone("UTC")
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.
When I try to parse Date object which has a format like 1950-01-01T00:00:00 then date time get it as yesterday.
i also tried to get time zone with DateTimeZone.getDefault() but its still not working.
Which timezone can I use to fix it?
DateTime dateTime = new DateTime(1950-01-01T00:00:00, DateTimeZone.UTC);
output:
dateTime.getDayOfMonth() = 31
dateTime.getMonthOfYear() = 12
dateTime.getYear() = 1949
Can you please help me?
Thanks.
fixed with adding below code in application class
DateTimeZone.setDefault(DateTimeZone.forID("Europe/Istanbul"));
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().
I have a problem that I want to add 15 days in my date string but I don't know How to do that? Please help me regarding that.
Thanks in advance
You must parse the date by means of a DateFormat, then use a GregorianCalendar to do the maths:
Date date = DateFormat.getDateFormat(this).parse("12/31/1999");
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.add(Calendar.DAY_OF_MONTH, 15);
You can use the add method. Create a calendar object with the current date. And use the below method.
This should give you an idea
DateFormat objFormatter = new SimpleDateFormat("dd-MM-yyyy");
objCalendar.add(Calendar.DATE, 15);
return objFormatter.format(objCalendarDup.getTime());
Turn it into a java.util.Date and then use either java.util.Calendar or JODA time to do i
it.
A Date isn't a String. Use the types that are available to you. Convert the String into a Date using java.text.DateFormat.