Unless if it is complicated (like Saj's List extension) or for organization purposes (packaging a lot of functions into an extension), there's no such need for test extensions. Like your previous extensions, please focus on something not achievable by blocks.
ok, I understand I will take your advice in my mind,
I am just a beginner at creating extensions.
thank @gordonlu310
over all this is source code :
package com.age.be.dev;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.util.Calendar;
@DesignerComponent(
version = 1,
description = "Calculate age based on a given date is created by Mr_koder https://www.youtube.com/channel/UCMjrKCO3Y8_i1I_WOw6QQzw",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://i.ibb.co/RCbDMbp/Icon.png")
@SimpleObject(external = true)
// Libraries
@UsesLibraries(libraries = "")
// Permissions
@UsesPermissions(permissionNames = "")
public class Age extends AndroidNonvisibleComponent {
// Activity and Context
private Context context;
private Activity activity;
public Age(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "Calculate age based on the provided birthday in the format yyyy-MM-dd")
public static int CalculateAge(String birthday) {
Calendar today = Calendar.getInstance();
Calendar birthdate = Calendar.getInstance();
birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));
int age = today.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < birthdate.get(Calendar.MONTH) || (today.get(Calendar.MONTH) == birthdate.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) < birthdate.get(Calendar.DAY_OF_MONTH))) {
age--;
}
return age;
}
@SimpleFunction(description = "Calculate age in days based on the provided birthday in the format yyyy-MM-dd")
public static int CalculateAgeInDays(String birthday) {
Calendar today = Calendar.getInstance();
Calendar birthdate = Calendar.getInstance();
birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));
long diffInMillis = today.getTimeInMillis() - birthdate.getTimeInMillis();
return (int) (diffInMillis / (1000 * 60 * 60 * 24));
}
@SimpleFunction(description = "Calculate age in months based on the provided birthday in the format yyyy-MM-dd")
public static int CalculateAgeInMonths(String birthday) {
Calendar today = Calendar.getInstance();
Calendar birthdate = Calendar.getInstance();
birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));
int years = today.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
int months = today.get(Calendar.MONTH) - birthdate.get(Calendar.MONTH);
return years * 12 + months;
}
@SimpleFunction(description = "Check if today is the birthday based on the provided birthday in the format yyyy-MM-dd")
public static boolean IsBirthdayToday(String birthday) {
Calendar today = Calendar.getInstance();
Calendar birthdate = Calendar.getInstance();
birthdate.set(Integer.parseInt(birthday.substring(0, 4)), Integer.parseInt(birthday.substring(5, 7)) - 1, Integer.parseInt(birthday.substring(8, 10)));
return (today.get(Calendar.MONTH) == birthdate.get(Calendar.MONTH)) && (today.get(Calendar.DAY_OF_MONTH) == birthdate.get(Calendar.DAY_OF_MONTH));
}
}