简体   繁体   中英

How to use a method from other class in android studio

I have 2 classes DateDifference.java and Others.java . My DateDifference.java looks like:

public class DateDifference {
    public static void main(String[] args) {
        // Creates two calendars instances
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        // Set the date for both of the calendar instance
        cal1.set(2006, Calendar.DECEMBER, 30);
        cal2.set(2007, Calendar.MAY, 3);

        // Get the represented date in milliseconds
        long millis1 = cal1.getTimeInMillis();
        long millis2 = cal2.getTimeInMillis();

        // Calculate difference in milliseconds
        long diff = millis2 - millis1;

        // Calculate difference in seconds
        long diffSeconds = diff / 1000;

        // Calculate difference in minutes
        long diffMinutes = diff / (60 * 1000);

        // Calculate difference in hours
        long diffHours = diff / (60 * 60 * 1000);

        // Calculate difference in days
        long diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.println("In milliseconds: " + diff + " milliseconds.");
        System.out.println("In seconds: " + diffSeconds + " seconds.");
        System.out.println("In minutes: " + diffMinutes + " minutes.");
        System.out.println("In hours: " + diffHours + " hours.");
        System.out.println("In days: " + diffDays + " days.");
    }
}

How am I able to print out all difference time in others.java or in other class?

You'll need to create a method other than main in DateDifference and call it from the class you want. eg:

public class DateDifference {

    /* 
    * If you're coding for Android you don't need the main function.
    * How to run an Android app is a completely different subject
    */

    public long getDifference(Calendar a, Calendar b) {
        long millisA = a.getTimeInMillis();
        long millisB = b.getTimeInMillis();

        return b - a;
    }

}

public class Others {

    // Say you want to display your diff here
    public void displayDiff() {
        DateDifference dateDifference = new DateDifference();

        Calendar a = Calendar.getInstance();
        Calendar b = Calendar.getInstance();

        a.set(2006, Calendar.DECEMBER, 30);
        b.set(2007, Calendar.MAY, 3);

        long diffMillis = dateDifference.getDifference(a, b);
        long diffSeconds = millisToSeconds(diffMillis);
        long diffMinutes = millisToMinutes(diffMillis);
        long diffHours = millisToHours(diffMillis);
        long diffDays = millisToDays(diffMillis);

        /*
        * If you're coding for Android you won't see this output in your app.
        * How to display data on an Android app is a completely different subject
        */
        System.out.println("In milliseconds: " + diffMillis + " milliseconds.");
        System.out.println("In seconds: " + diffSeconds + " seconds.");
        System.out.println("In minutes: " + diffMinutes + " minutes.");
        System.out.println("In hours: " + diffHours + " hours.");
        System.out.println("In days: " + diffDays + " days.");
    }

    private long millisToSeconds(long millis) {
        return millis / 1000;
    }

    private long millisToMinutes(long millis) {
        return diffMillis / (60 * 1000);
    }

    private long millisToHours(long millis) {
        return diffMillis / (60 * 60 * 1000);
    }

    private long millisToDays(long millis) {
        return diffMillis / (24 * 60 * 60 * 1000);
    }

}

If I understand your question correctly, you want to call codes in DateDifference from other class. You can create a static function in DateDifference.java and put your desired code there and call DateDifference.funstionName() from the class you want.

If you want to call a method in Others.java with the parameters you have, you can just call it with it's name and a "."

Others.whatever(millis1);

The whatever method has to be public and static . If you want to call the Variables from another methode, you can make them public

public class DateDifference {
        public long millis1;
        public  long millis2;
        public  long diff;
        public  long diffSeconds;
        public  long diffMinutes;
        public  long diffHours;
        public  long diffDays;
    public static void main(String[] args) {
        // Creates two calendars instances
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        // Set the date for both of the calendar instance
        cal1.set(2006, Calendar.DECEMBER, 30);
        cal2.set(2007, Calendar.MAY, 3);

        // Get the represented date in milliseconds
        millis1 = cal1.getTimeInMillis();
        millis2 = cal2.getTimeInMillis();

        // Calculate difference in milliseconds
        diff = millis2 - millis1;

        // Calculate difference in seconds
        diffSeconds = diff / 1000;

        // Calculate difference in minutes
        diffMinutes = diff / (60 * 1000);

        // Calculate difference in hours
        diffHours = diff / (60 * 60 * 1000);

        // Calculate difference in days
        diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.println("In milliseconds: " + diff + " milliseconds.");
        System.out.println("In seconds: " + diffSeconds + " seconds.");
        System.out.println("In minutes: " + diffMinutes + " minutes.");
        System.out.println("In hours: " + diffHours + " hours.");
        System.out.println("In days: " + diffDays + " days.");
    }
}

to access for example mills1:

DateDifference.mills1;

As you have mentioned calling function from another class in AS right? so What i have understood from your question so far is, you might have confused your activityClass with regular java class which is completely different.
Activity class just take it, as starter class for any app where everything is happening widgets, list, fragments etc. Whenever you open any app first thing you see is your activity. alright? And inside activityClass you only can do what android studio allows you. alright?
Question is:

Can you methods from other class in android studio?
yes!

class LoginActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);
OtherClass oC = new OtherClass();
oC.method();
  }
}

Other class

public class OtherClass{

public void method() {
system.out.println("Yes!!");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM