从特定日期获取日期、月份和年份
Get date , month and year from particular date
我想获取特定日期的日期、月份和年份。
我使用了以下代码:
String dob = "01/08/1990";
int month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = cal.get(Calendar.MONTH);
dd = cal.get(Calendar.DATE);
yer = cal.get(Calendar.YEAR);
} catch (Exception e) {
e.printStackTrace();
}
所以从上面的代码我得到 month -0 , yer - 1990 and date - 8
但是我想要month - 01 , date - 08 and yer - 1990
.
我也定义了日期格式,但我没有从日期、月份和年份中获得完美的值。
在 java.util.Calendar 中,月份是从零开始的,因此它为您提供正确的值。
检查日历中的常量class:
public final static int JANUARY = 0;
public final static int FEBRUARY = 1;
...
public final static int DECEMBER = 11;
public static void main (String[] args) throws java.lang.Exception
{
String dob = "01/08/1990";
int month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = cal.get(Calendar.MONTH);
dd = cal.get(Calendar.DATE);
yer = cal.get(Calendar.YEAR);
System.out.println("Month - " + String.format("%02d", (month+1)));
System.out.println("Day - " + String.format("%02d", dd));
System.out.println("Year - " + yer);
} catch (Exception e) {
e.printStackTrace();
}
}
Since months in the Calendar are considered from 0-11 so we need to add
1 to it.
我已经执行了一个示例,您可以查看 here
日历中的月份将从 0 到 11。您必须在月份中添加 +1。
String dob = "01/08/1990";
String month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = checkDigit(cal.get(Calendar.MONTH)+1);
dd = checkDigit(cal.get(Calendar.DATE));
yer = checkDigit(cal.get(Calendar.YEAR));
} catch (Exception e) {
e.printStackTrace();
}
用于添加前导零的 checkDigit 方法。 checkDigit() 方法 returns 字符串值。如果你想转换成整数那么你可以像 Integer.parseInt(YOUR_STRING);
// ADDS 0 e.g - 02 instead of 2
public String checkDigit (int number) {
return number <= 9 ? "0" + number : String.valueOf(number);
}
我想获取特定日期的日期、月份和年份。
我使用了以下代码:
String dob = "01/08/1990";
int month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = cal.get(Calendar.MONTH);
dd = cal.get(Calendar.DATE);
yer = cal.get(Calendar.YEAR);
} catch (Exception e) {
e.printStackTrace();
}
所以从上面的代码我得到 month -0 , yer - 1990 and date - 8
但是我想要month - 01 , date - 08 and yer - 1990
.
我也定义了日期格式,但我没有从日期、月份和年份中获得完美的值。
在 java.util.Calendar 中,月份是从零开始的,因此它为您提供正确的值。
检查日历中的常量class:
public final static int JANUARY = 0;
public final static int FEBRUARY = 1;
...
public final static int DECEMBER = 11;
public static void main (String[] args) throws java.lang.Exception
{
String dob = "01/08/1990";
int month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = cal.get(Calendar.MONTH);
dd = cal.get(Calendar.DATE);
yer = cal.get(Calendar.YEAR);
System.out.println("Month - " + String.format("%02d", (month+1)));
System.out.println("Day - " + String.format("%02d", dd));
System.out.println("Year - " + yer);
} catch (Exception e) {
e.printStackTrace();
}
}
Since months in the Calendar are considered from 0-11 so we need to add 1 to it.
我已经执行了一个示例,您可以查看 here
日历中的月份将从 0 到 11。您必须在月份中添加 +1。
String dob = "01/08/1990";
String month = 0, dd = 0, yer = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d = sdf.parse(dob);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
month = checkDigit(cal.get(Calendar.MONTH)+1);
dd = checkDigit(cal.get(Calendar.DATE));
yer = checkDigit(cal.get(Calendar.YEAR));
} catch (Exception e) {
e.printStackTrace();
}
用于添加前导零的 checkDigit 方法。 checkDigit() 方法 returns 字符串值。如果你想转换成整数那么你可以像 Integer.parseInt(YOUR_STRING);
// ADDS 0 e.g - 02 instead of 2
public String checkDigit (int number) {
return number <= 9 ? "0" + number : String.valueOf(number);
}