I would like to ask what is the most suitable way to create List of Dates for specific year to hold the dates of this year.
I have written the following and it works fine.
However,I am not sure that this the most convenient way.
Calendar cal = new GregorianCalendar(2012, Calendar.JANUARY, 1);
Date startDate = cal.getTime();
cal = new GregorianCalendar(2013, Calendar.JANUARY, 1);
Date endDate = cal.getTime();
List<Date> dates = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
while (calendar.getTime().before(endDate)) {
Date date= calendar.getTime();
dates.add(date);
calendar.add(Calendar.DATE, 1);
}
You could just simplify that:
int startYear = 2012;
int endYear = 2013;
Calendar cal = new GregorianCalendar(startYear, Calendar.JANUARY, 1);
ArrayList<Date> dates = new ArrayList<Date>();
while(cal.get(Calendar.YEAR) < endYear){
dates.add(cal.getTime());
cal.add(Calendar.DATE, 1);
}
Similar as how to get a list of dates between two dates in java
With Lamma one can easily construct date range:
for (Date d: Dates.from(2014, 6, 29).to(2014, 7, 1).build()) {
System.out.println(d);
}
And the output is:
Date(2014,6,29)
Date(2014,6,30)
Date(2014,7,1)
Related
I am using SimpleDateFormat to get the current month but i want to show a recyclerview table with information of this month and past three months.
My php json loads this but i want to put automatically it in android.
periodo1 = findViewById(R.id.tittle_periodo1);
periodo2 = findViewById(R.id.tittle_periodo2);
periodo3 = findViewById(R.id.tittle_periodo3);
periodo4 = findViewById(R.id.tittle_periodo4);
SimpleDateFormat month_date = new SimpleDateFormat("yyyyMM");
String month1 = month_date.format(c.getTime());
periodo1.setText(month1);
try this :
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -3); // -3 is Number of months past (july)
Date newDate = calendar.getTime();
and you can format it if you want :
String date = DateFormat.format("MM/dd/yyyy", newDate).toString();
With Java8 syntax you can use time library to achieve this
import java.time.LocalDate;
LocalDate now = LocalDate.now(); // 2019-11-01 (Nov)
LocalDate minusOneMonth = now.minusMonths(1); // 2019-10-01
minusOneMonth.getMonth().getValue(); // Gives -1 month (10)
LocalDate minusTwoMonth = now.minusMonths(2); // 2019-09-01
minusTwoMonth.getMonth().getValue(); // Gives -2 month (09)
Hope that's what you are looking for.
If you can't use Java8 syntax, use following
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -1);
Date newDate = cal.getTime();
.... = new SimpleDateFormat("M")
Basically, I've got a little program that uses date.
Date current = new Date();
current.setDate(current.getDay() + time1);
When I do this it adds to the day, but say time1 = 30 then the month doesn't change when I print the date out. I hope this makes sense I'm kinda new to this.
Use a Calendar to perform date arithmetic and a DateFormat to display the result. Something like,
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 30);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(cal.getTime()));
Use this method
public static Date addDaystoGivenDate(Integer days, Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
I need to add 28 days to a Date - I have tried this:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = df.parse("01/10/2012");
long week = 1000 * 60 * 24 * 7;
date1.setTime(date1.getTime() + week);
but I got an error on this line: Date date1 = df.parse("01/10/2012");
the error: Type mismatch: cannot convert from java.util.Date to java.sql.Date
I also tried this:
Date Mydate = new Date(02,04,2012);
Calendar cal = Calendar.getInstance();
cal.setTime(Mydate);
cal.add(Calendar.DATE, 10); // add 10 days
Mydate = (Date) cal.getTime();
but I got an error when trying to see the Mydate value.
You need to change this line:
import java.sql.Date;
to this:
import java.util.Date;
Once you've done that, I think the best approach is:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 3); // NOTE: 0 is January, 1 is February, etc.
cal.set(Calendar.DAY_OF_MONTH, 2);
cal.add(Calendar.DAY_OF_MONTH, 10); // add 10 days
Date date = cal.getTime();
This works for me:
public static void main(String args[]) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
Date date1 = df.parse("01/10/2012");
System.out.println(date1);
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DAY_OF_MONTH, 28); // add 28 days
date1 = (Date) cal.getTime();
System.out.println(date1);
}
Try code below:
Calendar MyDate= Calendar.getInstance();
long sum = MyDate.getTimeInMillis() + 2419200000; //28 days in milliseconds
MyDate.setTimeInMillis(sum);
I get the today's date like this:
final Calendar cal = Calendar.getInstance();
{
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
}
I want to calculate what was the date x days ago... anyone got something?
A better way would be to use add method instead of set:
cal.add(DAY_OF_YEAR, -2);
I.e. to be sure it works also the first day in month etc.
You can do the following :
Calendar cal=Calendar.getInstance();
int currentDay=cal.get(Calendar.DAY_OF_MONTH);
//Set the date to 2 days ago
cal.set(Calendar.DAY_OF_MONTH, currentDay-2);
then you can get the date :
cal.getTime(); //The date 2 days ago
I use the following fuction:
public static Date getStartOfDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static long getDaysAgo(Date date){
final long diff = getStartOfDay().getTime() - date.getTime();
if(diff < 0){
// if the input date millisecond > today's 12:00am millisecond it is today
// (this won't work if you input tomorrow)
return 0;
}else{
return TimeUnit.MILLISECONDS.toDays(diff)+1;
}
}
Same kind of code, but using the Joda-Time 2.3 library and Java 7.
DateTime dateTime = new DateTime( 2014, 2, 3, 7, 8, 9 );
DateTime twoDaysPrior = dateTime.minusDays( 2 );
dateTime: 2014-02-03T07:08:09.000-08:00
twoDaysPrior: 2014-02-01T07:08:09.000-08:00
I got a list of dates as String
for example date1->'11-11-2010' and date2->'12-01-2011'
I want to print all the dates between these two dates..
I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}