how to get current date and 7thday from current day in android? and I want to show the dates in below format given by the image
Asked Answered
P

4

6

In the given image first date is current date with month and day. and another date is 7th day from current date.

enter image description here

I tried with the below code::

 Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd.MM.yyyy");

            for (int i = 0; i < 7; i++) {
                Log.i("dateTag", sdf.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_WEEK, 1);
            }
        tvDate1=(TextView) findViewById(R.id.calender_view1);
        tvDate1.setText("" + DateFormat.format("dd/MM/yyyy", System.currentTimeMillis()));


tvDate2=(TextView) findViewById(R.id.calender_view2);
        tvDate2.setText("here want to print 7thday");
Pinnacle answered 13/12, 2015 at 11:25 Comment(1)
Your code is highly inefficient. Instead of a loop which adds 1 day 7 times, you should add 7 days in a single step.Encage
C
8
SimpleDateFormat dateFormat= new SimpleDateFormat("EEEE dd.MM.yyyy");
Calendar currentCal = Calendar.getInstance();
String currentdate=dateFormat.format(currentCal.getTime());
currentCal.add(Calendar.DATE, 7); 
String toDate=dateFormat.format(currentCal.getTime());
Connelly answered 13/12, 2015 at 11:40 Comment(2)
how can i calculate if i have another date ? @ConnellySwainson
@VishalPatoliyaツ use currentCal.setTime(Date obj). Pass your Date obj after creating calendar instance.Connelly
S
0

You need strings for each day, and then use a switch on the DAY_OF_WEEK.

Septennial answered 13/12, 2015 at 11:29 Comment(0)
S
0

Check This Java Class

import java.util.Calendar;
import java.util.TimeZone;

public class DateAndTimeArithmetic {

    public static void main(String args[]){

        //Java calendar in default timezone and default locale
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT"));

        System.out.println("current date: " + getDate(cal));


        //adding days into Date in Java
        cal.add(Calendar.DATE, 2);
        System.out.println("date after 2 days : " + getDate(cal));

        //subtracting days from Date in Java
        cal.add(Calendar.DATE, -2);
        System.out.println("date before 2 days : " + getDate(cal));


       //adding moths into Date
        cal.add(Calendar.MONTH, 5);
        System.out.println("date after 5 months : " + getDate(cal));

        //subtracting months from Date
        cal.add(Calendar.MONTH, -5);
        System.out.println("date before 5 months : " + getDate(cal));

        //adding year into Date
        cal.add(Calendar.YEAR, 5);
        System.out.println("date after 5 years : " + getDate(cal));

        //subtracting year from Date
        cal.add(Calendar.YEAR, -5);
        System.out.println("date before 5 years : " + getDate(cal));

        //date after 200 days from now, takes care of how many days are in month
        //for years calendar takes care of leap year as well
        cal.add(Calendar.DATE, 200);
        System.out.println("date after 200 days from today : " + getDate(cal));

        System.out.println("current time in GMT: " + getTime(cal));

        //adding hours into Date
        cal.add(Calendar.HOUR_OF_DAY, 3);
        System.out.println("Time after 3 hours : " + getTime(cal));

        //subtracting hours from Date time
        cal.add(Calendar.HOUR_OF_DAY, -3);
        System.out.println("Time before 3 hours : " + getTime(cal));

        //adding minutes into Date time
        cal.add(Calendar.MINUTE, 3);
        System.out.println("Time after 3 minutes : " + getTime(cal));

        //subtracting minutes from Date time
        cal.add(Calendar.HOUR_OF_DAY, -3);
        System.out.println("Time before 3 minuets : " + getTime(cal));

    }

    /**
     *
     * @return current Date from Calendar in dd/MM/yyyy format
     * adding 1 into month because Calendar month starts from zero
     */
    public static String getDate(Calendar cal){
        return "" + cal.get(Calendar.DATE) +"/" +
                (cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.YEAR);
    }

    /**
     *
     * @return current Date from Calendar in HH:mm:SS format
     *
     * adding 1 into month because Calendar month starts from zero
     */
    public static String getTime(Calendar cal){
        return "" + cal.get(Calendar.HOUR_OF_DAY) +":" +
                (cal.get(Calendar.MINUTE)) + ":" + cal.get(Calendar.SECOND);
    }

}

Output:
current date: 23/7/2012
date after 2 days : 25/7/2012
date before 2 days : 23/7/2012
date after 5 months : 23/12/2012
date before 5 months : 23/7/2012
date after 5 years : 23/7/2017
date before 5 years : 23/7/2012
date after 200 days from today : 8/2/2013
current time in GMT: 6:12:53
Time after 3 hours : 9:12:53
Time before 3 hours : 6:12:53
Time after 3 minutes : 6:15:53
Time before 3 minuets : 3:15:53

And if Need Some More ways to solve your probelem check this stackoverflow post How can I increment a date by one day in Java?

Stenger answered 13/12, 2015 at 15:34 Comment(0)
P
0

Avoid legacy classes

Never use the terribly flawed legacy date-time classes such as Calendar, SimpleDateFormat, Date, etc. These have been supplanted by the modern java.time classes defined in JSR 310.

java.time

Get today's date. This requires a time zone because for any given moment the date varies around the globe by time zone. It is "tomorrow" in Tokyo Japan while simultaneously "yesterday" in Toledo Ohio US.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;  // Or ZoneId.systemDefault()
LocalDate today = LocalDate.now( z ) ;

Add seven days.

LocalDate sevenDaysLater = today.plusDays( 7 ) ;

Generate localized text using DateTimeFormatter.ofLocalized…. Covered already in many other Questions.

Precaution answered 17/8 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.