给定一个字符串形式的日期,任务是编写一个Java程序来获取给定日期的日、月和年。
例子:
Input: date = “2020-07-18”
Output:
Day: 18
Month: July
Year: 2020
Explanation: The given date is ‘2020-07-18’, so the day is: 18, the month is: July, and the year is: 2020.
Input: date = “2018-05-10”
Output:
Day: 10
Month: May
Year: 2018
Explanation: The given date is ‘2018-05-10’, so the day is: 10, the month is: May, and the year is: 2018.
方法 1:在Java使用LocalDate 类:
- 这个想法是使用LocalDate 的方法 类以从日期中获取日、月和年。
- getDayOfMonth()方法返回给定日期表示的日期, getMonth()方法返回给定日期表示的月份, getYear()方法返回给定日期表示的年份。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
class GFG {
// Function to get day, month, and
// year from date
public static void
getDayMonthYear(String date)
{
// Get an instance of LocalTime
// from date
LocalDate currentDate
= LocalDate.parse(date);
// Get day from date
int day = currentDate.getDayOfMonth();
// Get month from date
Month month = currentDate.getMonth();
// Get year from date
int year = currentDate.getYear();
// Print the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
}
// Driver Code
public static void main(String args[])
{
// Given Date
String date = "2020-07-18";
// Function Call
getDayMonthYear(date);
}
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String args[])
{
// Creating a calendar object
Calendar cal
= new GregorianCalendar(
2020, 07, 18);
// Getting the values of day,
// month, and year from calendar
// object
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
// Printing the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
}
}
Java
// Java program for the above approach
class GFG {
// Function to get day, month, and
// year from date
public static void findDate(String date)
{
// Spiliting the given date by '-'
String dateParts[] = date.split("-");
// Getting day, month, and year
// from date
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
// Printing the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
}
// Driver Code
public static void
main(String args[])
{
// Given date
String date = "18-07-2020";
findDate(date);
}
}
输出:
Day: 18
Month: JULY
Year: 2020
方法 2:在Java使用Calendar 类:
- 这个想法是使用Calendar 类的get()方法从日期中获取日、月和年。
- get()方法接受一个整数类型的参数,并从给定日期返回传递字段的值。
- 它返回月份索引而不是月份名称。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String args[])
{
// Creating a calendar object
Calendar cal
= new GregorianCalendar(
2020, 07, 18);
// Getting the values of day,
// month, and year from calendar
// object
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
// Printing the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
}
}
输出:
Day: 18
Month: 7
Year: 2020
方法 3:在Java使用String.split() :
- 这个想法是使用String 类的 split() 方法。
- 它根据提供的模式拆分字符串并返回字符串数组。
下面是上述方法的实现:
Java
// Java program for the above approach
class GFG {
// Function to get day, month, and
// year from date
public static void findDate(String date)
{
// Spiliting the given date by '-'
String dateParts[] = date.split("-");
// Getting day, month, and year
// from date
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];
// Printing the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
}
// Driver Code
public static void
main(String args[])
{
// Given date
String date = "18-07-2020";
findDate(date);
}
}
输出:
Day: 18
Month: 07
Year: 2020
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live