如何使用可重用函数 - Android、Java
How to use reusable function - Android, Java
当 Calendar instance = Calendar.getInstance(); 时,我实现了小的 if-else 部分来获取月份的前三个字母;检索月份的整数值。我想将它用作可重用的功能。我已经尝试创建另一个 java 文件,但它需要工作。谁能告诉我如何使用它?
public class MstrMonth {
public MstrMonth() {}
public static String getCurrentMonth(String currentMonth) {
Calendar instance = Calendar.getInstance();
int mMonth = instance.get(Calendar.MONTH) + 1;
if(mMonth == 1){
currentMonth = "JAN";
}else if (mMonth == 2){
currentMonth = "FEB";
}else if (mMonth == 3){
currentMonth = "MAR";
}else if (mMonth == 4){
currentMonth = "APR";
}else if (mMonth == 5){
currentMonth = "MAY";
}else if (mMonth == 6){
currentMonth = "JUN";
}else if (mMonth == 7){
currentMonth = "JUL";
}else if (mMonth == 8){
currentMonth = "AUG";
}else if (mMonth == 9){
currentMonth = "SEP";
}else if (mMonth == 10){
currentMonth = "OCT";
}else if (mMonth == 11){
currentMonth = "NOV";
}else if (mMonth == 12){
currentMonth = "DEC";
}
return currentMonth;
}
}
主要activity;
String currentMonth;
MstrMonth.getCurrentMonth(currentMonth);
我想在这里获取月份代码,但它给出了空值。
试试这个代码,在我的测试项目中工作
public class MstrMonth {
public MstrMonth() {}
public static String getCurrentMonth() {
String currentMonth = null;
Calendar instance = Calendar.getInstance();
int mMonth = instance.get(Calendar.MONTH) + 1;
if(mMonth == 1){
currentMonth = "JAN";
}else if (mMonth == 2){
currentMonth = "FEB";
}else if (mMonth == 3){
currentMonth = "MAR";
}else if (mMonth == 4){
currentMonth = "APR";
}else if (mMonth == 5){
currentMonth = "MAY";
}else if (mMonth == 6){
currentMonth = "JUN";
}else if (mMonth == 7){
currentMonth = "JUL";
}else if (mMonth == 8){
currentMonth = "AUG";
}else if (mMonth == 9){
currentMonth = "SEP";
}else if (mMonth == 10){
currentMonth = "OCT";
}else if (mMonth == 11){
currentMonth = "NOV";
}else if (mMonth == 12){
currentMonth = "DEC";
}
return currentMonth;
}
}
在activity
String currentMonth = MstrMonth.getCurrentMonth(currentMonth);
试试这个。
public static String getCurrentMonth() {
Calendar instance = Calendar.getInstance();
return instance.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH).toUpperCase();
}
public static void main(String[] args) throws Exception {
String currentMonth = getCurrentMonth();
System.out.println(currentMonth);
}
输出:
NOV
当 Calendar instance = Calendar.getInstance(); 时,我实现了小的 if-else 部分来获取月份的前三个字母;检索月份的整数值。我想将它用作可重用的功能。我已经尝试创建另一个 java 文件,但它需要工作。谁能告诉我如何使用它?
public class MstrMonth {
public MstrMonth() {}
public static String getCurrentMonth(String currentMonth) {
Calendar instance = Calendar.getInstance();
int mMonth = instance.get(Calendar.MONTH) + 1;
if(mMonth == 1){
currentMonth = "JAN";
}else if (mMonth == 2){
currentMonth = "FEB";
}else if (mMonth == 3){
currentMonth = "MAR";
}else if (mMonth == 4){
currentMonth = "APR";
}else if (mMonth == 5){
currentMonth = "MAY";
}else if (mMonth == 6){
currentMonth = "JUN";
}else if (mMonth == 7){
currentMonth = "JUL";
}else if (mMonth == 8){
currentMonth = "AUG";
}else if (mMonth == 9){
currentMonth = "SEP";
}else if (mMonth == 10){
currentMonth = "OCT";
}else if (mMonth == 11){
currentMonth = "NOV";
}else if (mMonth == 12){
currentMonth = "DEC";
}
return currentMonth;
}
}
主要activity;
String currentMonth;
MstrMonth.getCurrentMonth(currentMonth);
我想在这里获取月份代码,但它给出了空值。
试试这个代码,在我的测试项目中工作
public class MstrMonth {
public MstrMonth() {}
public static String getCurrentMonth() {
String currentMonth = null;
Calendar instance = Calendar.getInstance();
int mMonth = instance.get(Calendar.MONTH) + 1;
if(mMonth == 1){
currentMonth = "JAN";
}else if (mMonth == 2){
currentMonth = "FEB";
}else if (mMonth == 3){
currentMonth = "MAR";
}else if (mMonth == 4){
currentMonth = "APR";
}else if (mMonth == 5){
currentMonth = "MAY";
}else if (mMonth == 6){
currentMonth = "JUN";
}else if (mMonth == 7){
currentMonth = "JUL";
}else if (mMonth == 8){
currentMonth = "AUG";
}else if (mMonth == 9){
currentMonth = "SEP";
}else if (mMonth == 10){
currentMonth = "OCT";
}else if (mMonth == 11){
currentMonth = "NOV";
}else if (mMonth == 12){
currentMonth = "DEC";
}
return currentMonth;
}
}
在activity
String currentMonth = MstrMonth.getCurrentMonth(currentMonth);
试试这个。
public static String getCurrentMonth() {
Calendar instance = Calendar.getInstance();
return instance.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH).toUpperCase();
}
public static void main(String[] args) throws Exception {
String currentMonth = getCurrentMonth();
System.out.println(currentMonth);
}
输出:
NOV