how do i set custom date in android - android

How do i set the date to 25 -12(december)- current year.
eg.
I am using this code
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.add(Calendar.YEAR,0);
currentDate.add(Calendar.MONTH, 12);
currentDate.add(Calendar.DATE,25);
return currentDate;
}

Something like this should work:
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.set(currentDate.get(Calendar.YEAR),Calendar.DECEMBER,25);
return currentDate;
}

You're trying to add 12 months, instead of setting the month to December (which is month 11, because the Java API is horrible). You want something like:
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.MONTH, 11); // Months are 0-based!
currentDate.set(Calendar.DAY_OF_MONTH, 25); // Clearer than DATE
return currentDate;
}

Use this it found very usefull to me though :
Take a look at SimpleDateFormat.
The basics for getting the current time in ISO8601 format:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
String now = df.format(new Date());
For other formats:
DateFormat df = new SimpleDateFormat("MMM d, yyyy");
String now = df.format(new Date());
or
DateFormat df = new SimpleDateFormat("MM/dd/yy");
String now = df.format(new Date());
EDit:
Check this link it will help you :
Specific date

Related

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

How to get only the year from the Date Class

Is there a way to get only the year from the Date class and parse it into a string?
Here is the code that I use:
Date d1 = new Date();
d1.getYear(); `
And how to set the date to DatTime.Now;
like in C#?
Use SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
System.out.println(sdf.format(new Date()));
Output:
2014
public static String getCurrentDate(String format) {
SimpleDateFormat sdfFrom = new SimpleDateFormat(format);
Calendar currentTime = Calendar.getInstance();
return (sdfFrom.format(currentTime.getTime()));
}
public static Date getCurrentDate() {
Calendar currentTime = Calendar.getInstance();
return currentTime.getTime();
}
try to use java 8 date

get current date and time

I'm kind of stuck on date and time.
I want my program to create the date like this "20121217". The first 4 letters are the year, the second 2 letters are the month and the last 2 are the day. year+month+day
The time is "112233" hour+minute+second
Thanks for your help!
That's a formatting issue. Java uses java.util.Date and java.text.DateFormat and java.text.SimpleDateFormat for those things.
DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd hhmmss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
You can do something like this:
Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DATE);
String time = c.get(Calendar.HOUR) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
Change any specific format of time or date as you need.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
currentDateandTime = sdf.format(new Date());
For date:
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String strDate = df.format(new Date());
For time:
DateFormat df = new SimpleDateFormat("hhmmss");
String strTime = df.format(new Date());
This works,
String currentDateTime;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateTime = sdf1.format(new Date());
What you're looking for is the SimpleDateFormat in Java... Take a look at this page.
Try this for your need:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss");
Date parsed = format.parse(new Date());
System.out.println(parsed.toString());
You can use following method to get current time
/**************************************************************
* getCurrentTime() it will return system time
*
* #return
****************************************************************/
public static String getCurrentTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime());
}// end of getCurrentTime()

Date retreival in android

In my application i have 1 edittext box,in this user will enter some date.What i want is i have to get the date of 7th day from the user entered date.I searched in google,i found 1 solution.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add("field", +7);
String currentDateandTime = sdf.format(cal.getTime());
In the above cal.add("field",+7)-->Field is int.But my date format is string.So i cant use here..Please help me..
Get date from SimpleDateFormat and add this date object to calender then change into calender. And again get new Updated date from Calender. Wait i will post code
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date UserEnterDate = sdf.parse("String from your editbox");
Calendar calendar = Calendar.getInstance();
calendar.setTime(UserEnterDate);
int day = calendar.get(Calendar.DAY_OF_MONTH);
day = day + 7;
calendar.set(Calendar.DAY_OF_MONTH, day);
String newDate = calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.MONTH) + "/"
+ calendar.get(Calendar.YEAR);
} catch (Exception e) {
// TODO: handle exception
}
As you said thatyou got the date in string format.
So let me start from there
Suppose the date is:
String dt = "2008-01-05"; // Start date
Then do thhis::
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 7); // number of days to add
dt = sdf.format(c.getTime());
System.out.println(""+dt);
Hope.this will definitely help you.
Enjoy!!!
I suggest better you use DatePickerDialog in onClick() of EditText
then you will get individual Date Month Year. Then you can set your date
Date+=7
you can get Date Object from a String if you know the format, by your question the format is:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add("field", +7);
String currentDateandTime = sdf.format(cal.getTime());
now use this date format to parse a string to get Date object, say:
Date dt=sdf.parse(txtDt.getText().toString());
now Set this date to Calendar Object:
Calendar cal=Calendar.getInstance();
cal.setTime(dt);
now you need to add 7 days to this date, so do as:
cal.add(Calendar.DAY_OF_MONTH, 7);
now you have been added 7 days to the date successfully, now get Date from this Calendar object, by using:
Date dtNew=cal.getTime();
and you can convert it to readable string using:
String strNewDt=sdf.format(dtNew);
You have to write the name of the field you want to modify to the "field" parameter, here#s a link to the calendar reference. The calendar object is not a string, but a whole different creature that youre using. Use its functions to do it.
http://developer.android.com/reference/java/util/Calendar.html
And so you would write
!edit, you actually have to set the calendar using ints and parse your string to match. use the set function from the calendar:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = new Calendar;
cal.set(int year, int month, int day, int hourOfDay, int minute)
cal.add(DATE, 7);
String currentDateandTime = sdf.format(cal.getTime());

getDate() does not return the exact date that system have?

i am implementing custom listview in android with image, text and date but when i call getDate method it will work but return 1/1/1970 instead of showing my system date?
You can just use new java.util.Date() and it will contain the actual date. You can also use Calendar.getInstance() or long time = System.currentTimeMillis(); new Date(time);
FYI,
getDate() => This method is deprecated. And it returns the day of the month.
You can use:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // you can mention any format which you want
String currentDateandTime = sdf.format(new Date());
Use this:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND)
android calender documentation
here is the complete solution.where you can get the date and also access it in the form of string.
private final static String getDateTime() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
Log.i("Log", "date is: "+df.format(new Date()));
return df.format(new Date());
}
you can try this
DateFormat date = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
Log.i("Log", "date is: "+date.format(new Date()));
date.format(new Date());
use this:
long dtMili = System.currentTimeMillis();
String date = new java.text.SimpleDateFormat("dd-MM-yy").format(new java.util.Date(dtMili));

Categories

Resources