Find Missing Dates Between Two Dates - java

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);
}

Related

SimpleDateFormat for past month, two months ago, three months ago

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")

Google Calendar Api - convert DataTime to Calendar object

I am trying to convert this DataTime object to Calendar object.
DateTime start = event.getStart().getDateTime();
DateTime format: 2015-11-11T19:25:55.000Z
I need format: "yyyy/MM/dd HH:mm:ss"
How can I cut TimeZone and convert it to Calendar object?
Original Google Calendar Api:
private List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
List<String> eventStrings = new ArrayList<String>();
Events events = mService.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
// All-day events don't have start times, so just use
// the start date.
start = event.getStart().getDate();
}
eventStrings.add(String.format("%s (%s)", event.getSummary(),start));
}
return eventStrings;
}
You can use SimpleDateFormat to parse your 2015-11-11T19:25:55.000Z into an actual Date object. Once you've got that Date object, it's as easy as:
Calendar calendar = Calendar.getInstance();
calendar.setTime(your_date_object);
I hope this helps!
I'm trying do somthing with it, but its not work.
DateTime start = event.getStart().getDateTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String newDate = dateFormat.format(start);
Maybe someone know why i can't parse this data
DateTime start = event.getStart().getDateTime();
String NewDate = String.format("(%s)", start);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.getDefault());
cal.setTime(sdf.parse(NewDate));
All the time i have unparseable date exception.
I tried formats:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ssZZZZZ
yyyy-MM-dd'T'HH:mm:ss.SSSZZ
This work for me:
SimpleDateFormat dtExibicao = new SimpleDateFormat("dd/MM/yyyy");
Date dtInicio = new Date(event.getStart().getDateTime().getValue());
String stInicio = dtExibicao.format(dtInicio);
If you wanna a java.util.Calendar you can use a #k3v1n4ud3 sample code:
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(dtInicio);

Date to Unix timestamp

I have a string that looks like "2015-06-07 16:01:33.0". I want to convert it into a unix timestamp. First I use Calendar utility to convert it into a Date object. How can I convert the date to epoch time?
String origintime = "2015-06-07 16:01:33.0";
String year = origintime.split(" ")[0].split("-")[0];
String month = origintime.split(" ")[0].split("-")[1];
String day = origintime.split(" ")[0].split("-")[2];
String hour = origintime.split(" ")[1].split(":")[0];
String mins = origintime.split(" ")[1].split(":")[1];
String secs = origintime.split(" ")[1].split(":")[2].replace(".0","");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
cal.set(Calendar.MONTH, Integer.parseInt(month));
cal.set(Calendar.YEAR, Integer.parseInt(year));
cal.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hour));
cal.set(Calendar.MINUTE,Integer.parseInt(mins));
cal.set(Calendar.SECOND,Integer.parseInt(secs));
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
System.out.println(strdate);
I think you can search for answers at StackOverflow, instead of posting a new question, since it make StackOverflow better, I just did a quick search and here they are:
Getting unix timestamp from Date()
or here is the code that straight forward:
Date currentDate = new Date();
currentDate.getTime() / 1000;
Recently, people prefer jodaTime:
DateTime dateTime = new DateTime();
long unix = dateTime.getMillis()/1000;

Changing the date

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();
}

Calculating Date in Java

Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);

Categories

Resources