//this month
SimpleDateFormat df_formonth = new SimpleDateFormat("MMM");
c.set(Calendar.MONTH, 5); //integer to be changed upon click - maybe month counter from now
String currmonth = df_formonth.format(c.getTime());
This should return June since we index months from 0 to 11
but it returns july
any solutions or other ways to fix this?
Because today's date is the 31st of August and June only has 30 days, the month is automatically incremented to the following month giving July.
To solve you can set the date before setting the month
c.set(Calendar.DATE, 30);
c.set(Calendar.MONTH, Calendar.JUNE);
Also I suggest using Calendar constants for clarity
Well known issue when you are working with dates at the end of the month (31st of Aug).
You should explicitly set the date.
For example read here for details:
http://www.coderanch.com/t/385083/java/java/java-util-Calendar-set
You can try the following:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.MONTH, Calendar.JUNE);
Related
How to get week of particular selected date?
For Example:
My week will start from Monday and ends on Sunday.
So lets say i have selected 25 July 2017. So i want what was the date on monday of that week and what is the date on upcoming Sunday of that week.
The answer should be :: Monday -- 24 July 2017 AND Sunday-- 30 July 2017.
I am not able to find a simple way to get it.
You can see this. It is for the present date.
Calendar cal = Calendar.getInstance();
int week = cal.get(Calendar.WEEK_OF_MONTH);
int day = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(day);
Date mondayDate = null;
if (day > 2) {
int monday = day - 2;
cal.add(Calendar.DATE, -monday);
mondayDate = cal.getTime();
} else {
// cal.add(Calendar.DATE,);
mondayDate = cal.getTime();
}
int sunday = 7 - day + 1;
cal.add(Calendar.DATE, +sunday);
Date sundaydate = cal.getTime();
System.out.println(mondayDate);
System.out.println(sundaydate);
}
In this, we are finding the day of the week.Today we will get
day=2.
Now for monday,we will first check days.
if day=1, means it is sunday.
if day=2, means it is monday.
so for day>2, we are getting date of (day-2) days back. For today, day=1. hence mondaydate= 23 July,2017.
Similarily for sunday, we are getting date of (7-day+1) days later. For today, sunday=5, so after +6, sundaydate= 31 july,2017
Hope this helps :)
You can get like this :
String date = (String) android.text.format.DateFormat.format("dd", date);
String dayOfTheWeek = (String) DateFormat.format("EEEE", date);
For next Sunday you can calculate as per dayOfTheWeek.
This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Closed 6 years ago.
I have the following piece of code.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY HH:mm");
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, 31);
cal.set(Calendar.HOUR_OF_DAY, 22);
Date start = cal.getTime();
System.out.println(dateFormat.format(start));
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 5);
Date end = cal.getTime();
System.out.println(dateFormat.format(end));
It prints:
31/12/2016 22:00
01/01/2016 05:00
I expect that the year of the second date is 2017. What is going on? I'm using Java 1.7.
The correct date format should be dd/MM/yyyy HH:mm, not dd/MM/YYYY HH:mm, note the lower case y.
With that it works correctly.
From the docs:
y Year
Y Week year
Explanation of the difference between year and week year (from here):
A week year is a year where all the weeks in the year are whole weeks.
[...] Basically, this guarantees that a program working on a week's
data will not transition between years. [...] this also means that the beginning of the year may not start on the first of January.
This is working fine with the following dataFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm");
I'm using Calendar API to manipulate date. Here is snippet of code i'm trying.
Date date=new SimpleDateFormat("dd/mm/yyyy").parse("05/10/2013");
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int month=cal.get(Calendar.MONTH);
Here i should get month as 9(Oct) but its returning 0(Jan).Even if I change date it still returns 0(Jan).
Why this is happening? Please help me.
use MM instead of mm small m gives Minute in hour and to get Month use capital M
Date date=new SimpleDateFormat("dd/MM/yyyy").parse("05/10/2013");
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int month=cal.get(Calendar.MONTH);
System.out.println("" + month);
Output:
9
Java Docs
sue MM instead of mm in the given date format. M specifies month while m specifies minute according to java doc.
Date date=new SimpleDateFormat("dd/MM/yyyy").parse("05/10/2013")
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int month=cal.get(Calendar.MONTH); // add +1 for correct month(10) otherwise it will be 9
How do I find out the last month and its year in Java?
e.g. If today is Oct. 10 2012, the result should be Month = 9 and Year = 2012. If today is Jan. 10 2013, the result should be Month = 12 and Year = 2012.
Your solution is here but instead of addition you need to use subtraction
c.add(Calendar.MONTH, -1);
Then you can call getter on the Calendar to acquire proper fields
int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero
int year = c.get(Calendar.YEAR);
java.time
Using java.time framework built into Java 8:
import java.time.LocalDate;
LocalDate now = LocalDate.now(); // 2015-11-24
LocalDate earlier = now.minusMonths(1); // 2015-10-24
earlier.getMonth(); // java.time.Month = OCTOBER
earlier.getMonth.getValue(); // 10
earlier.getYear(); // 2015
Use Joda Time Library. It is very easy to handle date, time, calender and locale with it and it will be integrated to java in version 8.
DateTime#minusMonths method would help you get previous month.
DateTime month = new DateTime().minusMonths (1);
you can use the Calendar class to do so:
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
System.out.println(format.format(cal.getTime()));
This prints : 2012.09.10 11:01 for actual date 2012.10.10 11:01
The simplest & least error prone approach is... Use Calendar's roll() method. Like this:
c.roll(Calendar.MONTH, false);
the roll method takes a boolean, which basically means roll the month up(true) or down(false)?
YearMonth class
You can use the java.time.YearMonth class, and its minusMonths method.
YearMonth lastMonth = YearMonth.now().minusMonths(1);
Calling toString gives you output in standard ISO 8601 format: yyyy-mm
You can access the parts, the year and the month. You may choose to use the Month enum object, or a mere int value 1-12 for the month.
int year = lastMonth.getYear() ;
int month = lastMonth.getMonthValue() ;
Month monthEnum = lastMonth.getMonth() ;
private static String getPreviousMonthDate(Date date){
final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DATE, -1);
Date preMonthDate = cal.getTime();
return format.format(preMonthDate);
}
private static String getPreToPreMonthDate(Date date){
final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH,1);
cal.add(Calendar.DATE, -1);
Date preToPreMonthDate = cal.getTime();
return format.format(preToPreMonthDate);
}
You need to be aware that month is zero based so when you do the getMonth you will need to add 1. In the example below we have to add 1 to Januaray as 1 and not 0
Calendar c = Calendar.getInstance();
c.set(2011, 2, 1);
c.add(Calendar.MONTH, -1);
int month = c.get(Calendar.MONTH) + 1;
assertEquals(1, month);
You get by using the LocalDate class.
For Example:
To get last month date:
LocalDate.now().minusMonths(1);
To get starting date of last month
LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
Similarly for Year:
To get last year date:
LocalDate.now().minusYears(1);
To get starting date of last year :
LocalDate.now().minusYears(1).with(TemporalAdjusters.lastDayOfYear());
Here's the code snippet.I think it works.
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleMonth=new SimpleDateFormat("MMMM YYYY");
cal.add(Calendar.MONTH, -1);
System.out.println(simpleMonth.format(prevcal.getTime()));
I have such code:
Log.d(TAG, "day=%d, month=%d, year=%s", day, month, year);
Calendar c = Calendar.getInstance();
c.clear();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
Log.i(TAG, "Date is parsed to %tF", c.getTime(), c.get(Calendar.DAY_OF_MONTH));
And this is log I get when executing:
day=11, month=11, year=1985
Date is parsed to 1985-12-10
Why not 1985-12-11? It works correct for some dates or in debug mode. But why it is not always working?
I also have similar issues when working with Date and when parsing dates from String via SimpleDateFormat
EDIT: Other examples of this code executing:
day=1, month=0, year=2012
Date is parsed to 2012-01-01
day=25, month=11, year=2011
Date is parsed to 2011-12-25
day=4, month=10, year=1979
Date is parsed to 1979-11-03
day=3, month=11, year=1984
Date is parsed to 1984-12-02
day, month and year can't be changed from other threads.
The month in Calendar is zero based. See here: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#MONTH
I suggest you read this: http://mindprod.com/jgloss/gregoriancalendar.html
If you can, it's actually easier and less bug prone to use joda-time - it has a much neater and safer API.
Month is 0-11 and day starts with 1.
Month
http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#MONTH
Day
http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#DAY_OF_MONTH
Edit:
Calendar c = Calendar.getInstance();
c.clear();
c.set(Calendar.YEAR, 1985);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DAY_OF_MONTH, 11);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String strdate = sdf.format(c.getTime());
System.out.println(strdate);
Output:
12/11/1985
Try this:
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("MM-dd-yyyy");
Log.v("the date is:", simepleDateFormat.format(c.getTime());
Thanks for the help. It seems to be some Android devices issue. It has appeared in 3 of 5 devices. I have fixed it by specifying time.
c.set(Calendar.HOUR_OF_DAY, 12);
c.set(Calendar.MINUTE, 30);