写一个显示当前日期和时间的程序,写出代码Java
时间: 2023-07-16 14:13:25 浏览: 74
以下是Java代码实现:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DisplayDateTime {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = currentDateTime.format(formatter);
System.out.println("当前日期和时间为:" + formattedDateTime);
}
}
```
该程序使用了Java 8中的`LocalDateTime`类和`DateTimeFormatter`类,获取当前日期和时间并格式化输出。
相关问题
写一个日历的java程序
### 回答1:
你好,我可以为你提供一个简单的java程序来创建一个日历。以下是程序的代码:
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("当前日期: " + cal.getTime());
// add 10 days to the calendar
cal.add(Calendar.DATE, 10);
System.out.println("十天后的日期: " + cal.getTime());
}
}
### 回答2:
首先,让我们来编写一个简单的java日历程序。
```java
import java.time.LocalDate;
public class CalendarProgram {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前月份和年份
int currentMonth = currentDate.getMonthValue();
int currentYear = currentDate.getYear();
// 打印日历标题
System.out.println(" " + currentMonth + "月 " + currentYear);
System.out.println("日 一 二 三 四 五 六");
// 获取当前月份的第一天,并确定它是星期几
LocalDate firstDayOfMonth = LocalDate.of(currentYear, currentMonth, 1);
int dayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
// 打印日历
for (int i = 1; i < dayOfWeek; i++) {
System.out.print(" ");
}
int daysInMonth = currentDate.lengthOfMonth();
for (int i = 1; i <= daysInMonth; i++) {
System.out.printf("%2d ", i);
if ((i + dayOfWeek - 1) % 7 == 0) {
System.out.println();
}
}
}
}
```
这个程序使用了java.time包中的LocalDate类来获取当前日期,然后通过当前日期获取当前月份和年份。接下来,程序通过LocalDate类的静态方法of()来获取当前月份的第一天,并使用getDayOfWeek().getValue()方法获取它是星期几。根据这个星期几的值,程序在打印日历之前打印出一个空格。
程序接下来使用currentDate.lengthOfMonth()方法获取当前月份的天数,并使用for循环依次打印每一天的日期。如果当前日期是一个星期的最后一天,程序会换行打印下一行。
最后,程序会输出一个形如" 3月 2022"的标题,并在打印星期标题之后打印出日历。
### 回答3:
日历是一种记录日期的工具,我们可以使用Java编写一个简单的日历程序。下面是一个示例代码:
```java
import java.util.Calendar;
public class CalendarProgram {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; //月份从0开始,需要加1
int day = calendar.get(Calendar.DAY_OF_MONTH);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 打印日历的月份和年份
System.out.println(year + "年" + month + "月");
// 打印日历的表头
System.out.println("日 一 二 三 四 五 六");
// 计算该月份第一天是星期几
calendar.set(Calendar.DAY_OF_MONTH, 1);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// 打印日历的空白格
for (int i = 1; i < firstDayOfWeek; i++) {
System.out.print(" ");
}
// 打印日历的日期
for (int i = 1; i <= daysInMonth; i++) {
System.out.printf("%2d ", i);
// 每行打印7个日期
if ((firstDayOfWeek + i - 1) % 7 == 0) {
System.out.println();
}
}
}
}
```
这个程序会打印当前月份的日历。它首先获取当前的年份、月份和日期,并计算出这个月份有多少天。然后,它打印出月份和年份,并在第一行打印出日历的表头。接下来,它计算出这个月份的第一天是星期几,并打印出日历的空白格。最后,它打印出这个月份的日期,并确保每行打印7个日期。
用java写一个带有日程提醒功能的桌面日历程序
好的,我可以为你提供一个简单的桌面日历程序的实现思路和部分代码,你可以在此基础上进行修改和完善。
1. 界面设计
我们可以使用Java Swing库来实现程序的界面设计。桌面日历程序需要显示当前日期和月份的日历,还需要有添加、删除和编辑日程的功能。我们可以使用表格控件来显示日历,按钮控件来实现添加、删除和编辑功能。同时,我们还需要使用日期选择器控件来方便用户选择日期。
2. 实现日历显示
我们可以使用Calendar类来获取当前日期和月份,并根据这些信息来生成日历。具体实现过程如下:
```java
// 获取当前日期和月份
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
// 获取当前月份的第一天和最后一天
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = cal.getTime();
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
Date lastDayOfMonth = cal.getTime();
// 生成日历数组
String[][] calendarData = new String[6][7];
cal.setTime(firstDayOfMonth);
int row = 0, col = cal.get(Calendar.DAY_OF_WEEK) - 1;
while (cal.getTime().before(lastDayOfMonth) || cal.getTime().equals(lastDayOfMonth)) {
calendarData[row][col] = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
col++;
if (col >= 7) {
row++;
col = 0;
}
cal.add(Calendar.DAY_OF_MONTH, 1);
}
```
在上述代码中,我们首先使用Calendar类获取当前日期和月份。接着,我们获取当前月份的第一天和最后一天,并使用一个二维数组来存储日历数据。最后,我们使用循环来生成日历数据,其中row和col表示当前单元格的行和列。
3. 实现添加、删除和编辑日程
我们可以使用JOptionPane来弹出对话框,让用户输入日程信息,并使用ArrayList来保存日程列表。具体实现过程如下:
```java
// 添加日程
String schedule = JOptionPane.showInputDialog("请输入日程:");
if (schedule != null && !schedule.isEmpty()) {
int dayOfMonth = Integer.parseInt(calendarData[row][col]);
String date = String.format("%04d-%02d-%02d", year, month + 1, dayOfMonth);
String time = JOptionPane.showInputDialog("请输入时间(格式:HH:mm):");
String dateTime = date + " " + time + ":00";
scheduleList.add(dateTime + " " + schedule);
}
// 删除日程
if (calendarData[row][col] != null) {
ArrayList<String> selectedScheduleList = new ArrayList<>();
for (String item : scheduleList) {
if (item.startsWith(date + " ")) {
selectedScheduleList.add(item.substring(11));
}
}
if (!selectedScheduleList.isEmpty()) {
String[] options = selectedScheduleList.toArray(new String[selectedScheduleList.size()]);
int result = JOptionPane.showOptionDialog(null, "请选择要删除的日程:", "删除日程", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (result != JOptionPane.CLOSED_OPTION) {
String selectedSchedule = options[result];
scheduleList.removeIf(item -> item.startsWith(date + " " + selectedSchedule));
}
}
}
// 编辑日程
if (calendarData[row][col] != null) {
ArrayList<String> selectedScheduleList = new ArrayList<>();
for (String item : scheduleList) {
if (item.startsWith(date + " ")) {
selectedScheduleList.add(item.substring(11));
}
}
if (!selectedScheduleList.isEmpty()) {
String[] options = selectedScheduleList.toArray(new String[selectedScheduleList.size()]);
int result = JOptionPane.showOptionDialog(null, "请选择要编辑的日程:", "编辑日程", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (result != JOptionPane.CLOSED_OPTION) {
String selectedSchedule = options[result];
String newSchedule = JOptionPane.showInputDialog("请输入新的日程:", selectedSchedule);
if (newSchedule != null && !newSchedule.isEmpty()) {
scheduleList.replaceAll(item -> item.equals(date + " " + selectedSchedule) ? date + " " + newSchedule : item);
}
}
}
}
```
在上述代码中,我们首先判断用户是否点击了添加、删除或编辑按钮。接着,我们根据当前单元格的日期来查询日程列表,并弹出相应的对话框。最后,我们将用户输入的日程信息保存到ArrayList中,并使用lambda表达式来实现日程的删除和编辑功能。
4. 实现日程提醒
我们可以使用ScheduledExecutorService来实现日程提醒功能。具体实现过程如下:
```java
// 启动定时器
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(() -> {
Date now = new Date();
for (String item : scheduleList) {
Date scheduleDate = null;
try {
scheduleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(item.substring(0, 19));
} catch (ParseException e) {
e.printStackTrace();
}
if (scheduleDate != null && scheduleDate.after(now) && scheduleDate.getTime() - now.getTime() <= 60 * 1000) {
String schedule = item.substring(20);
JOptionPane.showMessageDialog(null, "日程提醒:" + schedule);
}
}
}, 0, 1, TimeUnit.MINUTES);
```
在上述代码中,我们首先创建一个ScheduledExecutorService对象,并使用scheduleAtFixedRate方法来执行定时任务。在定时任务中,我们首先获取当前时间,然后遍历日程列表,判断是否有日程需要提醒。如果找到需要提醒的日程,则弹出对话框显示提醒内容。
完整代码如下:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)