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.
Related
Can anyone show me how to format the MediaStore.Video.Media.DATE_MODIFIED to the human readable date format. I read in the docs that the time is in seconds. So i simply multiply it by 1000 and use the SimpleDateFormat . Here is my code
Date d=new Date(mills*1000);
DateFormat df=new SimpleDateFormat("dd-mm-yyyy");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(d);
This returns a String 25-35-2012 that off course is not correct. Any help with this ?
Kind Regards
you should use capital M, m refers to minute in hour while M refer to Month in year.
see this for further info
I'm a newbie to android and struggling to achieve this.
I want to select date and time and also wants to add the selected date and time in one textfield with this format dd/mm/yyyy hh:mm.
I searched on this but couldn't find what I want to achieve.
any guidelines in terms of code or link would be appreciated.
Thanks
you can use SimpleDateFormat by which you can format the Date.
String format = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(yourdate));
use this you can your desired format
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
like this May 17, 2012 12:03:09 PM
If you need to make Custom Date and Time Picker then you can see how in this they have make.Android Date Slider
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);
I"m following a tutorial where they have this line:
int callDate = c.getInt(dateColumn);
Next, they pass the callDate into the Date object to get a readable format.
But, I think the example may be old, because I get a compile error.
String myDate = DateUtils.dateString(callDate).toString();;
So, I'm looking at the API. how do I do it.
http://developer.android.com/reference/java/util/Date.html
String myString = DateFormat.getDateInstance().format(new Date());
or
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
Look at DateFormat and SimpleDateFormat.
You might want to use SimpleDateFormat for that
If I have understood your question correctly, you need to get the date in readable format.In that case you already have the answer you just need to read the link http://developer.android.com/reference/java/util/Date.html carefully for finding the answer. Let me provide you few hint for the same, see for the constructors on this link for initializing the date of your choice. The methods given helps you perform the operations on it. To answer your question specifically refer below snippet.
Date d1=new Date(); //Initializes this Date instance to the current time.
String date= d1.toString(); //this String var date can be used for printing date in readable format
the format of string will be "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999".
See the below link. It will definitely help.
Simple java date conversion
the problem is that you are getting from a Integer
int callDate = c.getInt(dateColumn);
the date is a Long type and you're losing precision on this conversion.
try changing to: long callDate = c.getLong(dateColumn);
hi i have a small question please i am new to android and have a date and time stamp
which looks like this yyyy-mm-dd hh:mm:ss
and i want to insert it into an sqlite table then read it back and compare it to the current time
any suggestions or examples
i found "SimpleDateFormat" but was not sure how to use it....?
thanks a lot
Have you considered using Calendar.getInstance.getTimeInMillis()? It's just the long representation of a date in milliseconds since Jan. 1, 1970. Great thing is your data is being stored in a format agnostic way. Just when you need to display it use SimpleDateFormat however you'd like.
Here is what you would do:
String myDate = new String("your date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(myDate);
date.getTime(); //fetch the time as milliseconds from Jan 1, 1970
Try use
System.currentTimeMillis();
its undepend from Calender.