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
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")
Given the assignment t = System.currentTimeMillis(), accrued at some point in the past, how do I get the millis of the same day as t in 12 pm and the day after 12 pm?
Note: this is timezone dependent. You can do as such:
import static java.util.Calendar.*;
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(yourValue);
cal.set(HOUR_OF_DAY, 12);
cal.set(MINUTE, 0);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
// cal.getTimeInMillis() contains the wanted day at 12pm
cal.add(DAY_OF_YEAR, 1);
// cal.getTimeInMillis() now contains the wanted day plus one at 12pm
But do yourself a favour and use Joda Time, it is much easier to use in this case:
final DateTime dayAt12pm = new DateTime(yourValue).toDateMidnight()
.plusHours(12);
// dayAt12pm.getMillis() contains the wanted day at 12pm
// next day at 12pm: dayAt12pm.plusDays(1).getMillis()
//plug your "T" here.
long t = System.currentTimeMillis();
Date date = new Date(t);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar startOfDay = Calendar.getInstance();
startOfDay.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
startOfDay.set(Calendar.HOUR, 12);
startOfDay.set(Calendar.MINUTE, 0);
startOfDay.set(Calendar.SECOND, 0);
startOfDay.set(Calendar.MILLISECOND, 0);
Date startOfDayDate = startOfDay.getTime();
System.err.println("12PM on time t is " + startOfDayDate);
startOfDay.add(Calendar.HOUR, 24);
startOfDayDate = startOfDay.getTime();
System.err.println("12PM day after t is " + startOfDayDate);
One way would be to create a Date from it and then use the Calendar class.
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 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)
I want jan 1st 2012 as long value from calendar object as a start time how to get this,i am doing as below
Calendar today = Calendar.getInstance();
today.set(Calendar.MILLISECOND, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.HOUR, 0);
long startTime = today.getTime().getTime();
Try this:
Calendar firstOfYear = Calendar.getInstance();
firstOfYear.clear();
firstOfYear.set(Calendar.YEAR, 2012);
firstOfYear.set(Calendar.DAY_OF_YEAR,1); //first day of the year.
this is the another way
String str_date="01-JAN-2012";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yyyy");
date = (Date)formatter.parse(str_date);
Calendar cal=Calendar.getInstance();
cal.setTime(date);
System.out.println("=======>"+cal.getTimeInMillis());