How do i get only the year part of a date using DateUtils ? I tried using
DateUtils.FORMAT_SHOW_YEAR
but this seems to return even the month and date.
No need to reinvent the wheel
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(new Date());
To take the locale into account use the constructor SimpleDateFormat("template", Locale)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
use DateFormat "yyyy", you will get year only
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date)); // sdf is SimpleDateFormat
System.println(c.get(Calendar.YEAR)));
you can use
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
you can check here
Related
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
This question already has answers here:
How to get the day, year, hours, min Individually from date format "yyyy-MM-dd'T'HH:mm:ss.SSSZ"?
(4 answers)
Closed 9 years ago.
specific date is "2013-11-12".
I want to extract day, month and year from above date. Please let me know how can I extract?
You can use split().
Example :
String mydate="2013-11-12"; //year-month-day
String myyear=mydate.split("-")[0]; //0th index = 2013
String mymonth=mydate.split("-")[1]; //1st index = 11
String myday=mydate.split("-")[2]; //2nd index = 12
you can use substring method to extract the particular characters from the above string like:
String year=date.substring(0,4); //this will return 2013
String month=date.substring(5,7); //this will return 11
String day=date.substring(8,10); //this will return 12
You can also use Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse("2013-11-12"));
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
You can do it using SimpleDateFormat and Calendar Class.
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
Now you can use this cal Object to do whatever you want. Not just the date, month or year. you cam use it to perform various operations, such as add month, add year etc...
Create date object from your date string first, like...
String yourDateString = "2013-11-12";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date yourDate = parser.parse(yourDateString);
Now create Calender instance to get further information about date...
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
int months = calendar.get(Calendar.DAY_OF_MONTH);
int seconds = calendar.get(Calendar.SECOND);
// and similarly use calender.getXXX
Hope this helps...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date testDate = null;
try {
testDate = sdf.parse("2013-11-12");
}
catch(Exception ex) {
ex.printStackTrace();
}
int date= testDate.getDate();
int month = testDate.getMonth();
int year = testDate.getYear();
I have this function
private void setDateInTopBar() {
Date bodyPartDate = DataManager.instance().getSelectedBodyPart().getPublicationDate();
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String formattedDate = format.format(bodyPartDate);
TextView dateTxt = (TextView) findViewById(R.id.txtImageDate);
dateTxt.setText(formattedDate);
}
but my month is 0, when day and year is working well, what's the problem?
Replace:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
With:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
as mm referes to minutes.. check http://developer.android.com/reference/java/text/SimpleDateFormat.html
See http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
lower-case m is for minutes.
Try: "dd/MM/yyyy"
I should set MM not mm here
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
mm means Minutes, So you shouldn't use it for Month, change mm to MM.
Your code must be like this:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Instead of this:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
You can read all about these letters here
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
In my application i want to add 12 hours with the current date and time,then i have to show it in this format "yyyy-MM-dd HH:mm:ss". I wrote the code but unable to add 12 hours. How can i do? please help.
My code is :
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
The Calendar class has the add method which you can use to add certain units.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12);
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
How about
Calendar cal = Calendar.getInstance();
Date date = cal.getTime()+12*60*60*1000;
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
You should add the 12 hours to the Calendar object like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12)
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);