日历源代码——java
- 格式:doc
- 大小:27.50 KB
- 文档页数:3
Dim x As Variant, y As Variant, z As Integer, m As Boolean Private Sub Command1_Click()Text1.Text = ""Text3.Text = ""Text4.Text = ""Text1.Enabled = TrueText3.Enabled = TrueText4.Enabled = TrueText1.SetFocusEnd SubPrivate Sub Command2_Click()Text2.Text = DateSelect Case (Weekday(Date) - 1)Case 0Text2.Text = Text2.Text + " 星期日"Case 1Text2.Text = Text2.Text + " 星期一"Case 2Text2.Text = Text2.Text + " 星期二"Case 3Text2.Text = Text2.Text + " 星期三"Case 4Text2.Text = Text2.Text + " 星期四"Case 5Text2.Text = Text2.Text + " 星期五"Case 6Text2.Text = Text2.Text + " 星期六"End SelectText1.Text = ""Text3.Text = ""Text4.Text = ""Text1.Enabled = FalseText3.Enabled = FalseText4.Enabled = FalseEnd SubPrivate Sub Text1_Change()If Len(Text1.Text) = 4 ThenText3.SetFocusEnd IfEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)Select Case KeyAsciiCase Asc("0") To Asc("9")Case ElseKeyAscii = 0End SelectEnd SubPrivate Sub Text2_GotFocus()Text2.Text = DateSelect Case (Weekday(Date) - 1)Case 0Text2.Text = Text2.Text + " 星期日"Case 1Text2.Text = Text2.Text + " 星期一"Case 2Text2.Text = Text2.Text + " 星期二"Case 3Text2.Text = Text2.Text + " 星期三"Case 4Text2.Text = Text2.Text + " 星期四"Case 5Text2.Text = Text2.Text + " 星期五"Case 6Text2.Text = Text2.Text + " 星期六"End SelectText1.Text = ""Text3.Text = ""Text4.Text = ""Text1.Enabled = FalseText3.Enabled = FalseText4.Enabled = FalseEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) Select Case KeyAsciiCase 48 To 57Case ElseKeyAscii = 0End SelectEnd SubPrivate Sub Text3_Change()Dim c As Integerc = Val(Text3.Text)If c <= 12 And Len(Trim(Text3.Text)) = 2 ThenText4.SetFocusElseIf Val(Text3.Text) >= 13 ThenText2.Text = "没有这一个月份"Text3.Text = ""End IfEnd SubPrivate Sub Text3_KeyPress(KeyAscii As Integer)Select Case KeyAsciiCase 48 To 57Case ElseKeyAscii = 0End SelectEnd SubPrivate Sub Text4_Change()Select Case Text3.TextCase "01", "03", "05", "07", "08", "10", "12"If Val(Text4.Text) <= 31 And Len(Trim(Text4.Text)) = 2 Then x = DateSerial(Text1.Text, Text3.Text, Text4.Text)y = Weekday(x) - 1Select Case (y)Case 0Text2.Text = "星期日"Case 1Text2.Text = "星期一"Case 2Text2.Text = "星期二"Case 3Text2.Text = "星期三"Case 4Text2.Text = "星期四"Case 5Text2.Text = "星期五"Case 6Text2.Text = "星期六"End SelectElseIf Val(Text4.Text) >= 32 ThenText2.Text = "没有这一天"Text4.Text = ""End IfCase "04", "06", "09", "11"If Val(Text4.Text) <= 30 And Len(Text4.Text) = 2 Thenx = DateSerial(Text1.Text, Text3.Text, Text4.Text)y = Weekday(x) - 1Select Case (y)Case 0Text2.Text = "星期日"Case 1Text2.Text = "星期一"Case 2Text2.Text = "星期二"Case 3Text2.Text = "星期三"Case 4Text2.Text = "星期四"Case 5Text2.Text = "星期五"Case 6Text2.Text = "星期六"End SelectElseIf Val(Text4.Text) >= 31 ThenText2.Text = "没有这一天"Text4.Text = ""End IfCase "02"Dim t As Integert = Text1.TextIf t Mod 100 = 0 ThenIf t Mod 400 = 0 Thenm = TrueElse: m = FalseEnd IfElseIf t Mod 4 = 0 Thenm = TrueElse: m = FalseEnd IfIf m = 1 ThenIf Val(Text4.Text) <= 29 And Len(Text4.Text) = 2 Thenx = DateSerial(Text1.Text, Text3.Text, Text4.Text)y = Weekday(x) - 1Select Case (y)Case 0Text2.Text = "星期日"Case 1Text2.Text = "星期一"Case 2Text2.Text = "星期二"Case 3Text2.Text = "星期三"Case 4Text2.Text = "星期四"Case 5Text2.Text = "星期五"Case 6Text2.Text = "星期六"End SelectElseIf Val(Text4.Text) >= 30 ThenText2.Text = "没有这一天"Text4.Text = ""End IfElseIf Val(Text4.Text) <= 28 And Len(Text4.Text) = 2 Thenx = DateSerial(Text1.Text, Text3.Text, Text4.Text)y = Weekday(x) - 1Select Case (y)Case 0Text2.Text = "星期日"Case 1Text2.Text = "星期一"Case 2Text2.Text = "星期二"Case 3Text2.Text = "星期三"Case 4Text2.Text = "星期四"Case 5Text2.Text = "星期五"Case 6Text2.Text = "星期六"End SelectElseIf Val(Text4.Text) >= 29 ThenText2.Text = "没有这一天"Text4.Text = ""End IfEnd IfEnd SelectEnd Sub。
JA V A处理日期时间常用方法:java.util.CalendarCalendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期提供了一些方法。
瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间1970 年 1 月1 日的00:00:00。
000,格里高利历的偏移量.例:Calendar cal = Calendar。
getInstance(;//使用默认时区和语言环境获得一个日历。
cal。
add(Calendar.DAY_OF_MONTH, -1;//取当前日期的前一天。
cal。
add(Calendar。
DAY_OF_MONTH, +1;//取当前日期的后一天。
//通过格式化输出日期java.text.SimpleDateFormat format = new java.text。
SimpleDateFormat(”yyyy-MM—dd";System。
out.println("Today is:”+format。
format(Calendar.getInstance(.getTime (;System。
out.println("yesterday is:"+format.format(cal.getTime(;得到2007—12-25日期: Calendar calendar = new GregorianCalendar(2007,11, 25,0,0,0;Date date = calendar.getTime(;System。
out.println("2007 Christmasis:”+format.format(date;//java月份是从0—11,月份设置时要减1。
//GregorianCalendar构造方法参数依次为:年,月—1,日,时,分,秒。
java获取年月日的方法Java是一种面向对象的编程语言,广泛应用于软件开发领域。
在Java中,获取年月日是一项常见的操作,可以通过多种方法实现。
下面,我将为大家介绍一些常用的获取年月日的方法,并解释它们的使用方式。
一、使用Java内置的日期类Java提供了多个日期类,其中最常用的是`Date`和`Calendar`类。
这两个类都可以用于获取年月日,但它们的使用方式有所不同。
1. `Date`类`Date`类是`java.util`包中的一个类,可以表示一个特定的时间点。
要获取当前的年月日,可以使用`Date`类的`getYear()`、`getMonth()`和`getDate()`方法。
需要注意的是,`getYear()`方法返回的年份是相对于1900的偏移量,所以需要将其加上1900才能得到真实的年份。
```javaDate currentDate = new Date();int year = currentDate.getYear() + 1900;int month = currentDate.getMonth() + 1;int day = currentDate.getDate();System.out.println("当前日期:" + year + "年" + month + "月" + day + "日");```2. `Calendar`类`Calendar`类是`java.util`包中的一个抽象类,它提供了丰富的日期操作方法。
与`Date`类相比,`Calendar`类更常用,因为它可以处理更复杂的日期计算。
要获取当前的年月日,可以使用`Calendar`类的`get()`方法,并传入相应的`Calendar`字段参数,如`Calendar.YEAR`、`Calendar.MONTH`和`Calendar.DAY_OF_MONTH`。
Java获取年月日的方法在Java编程中,我们经常需要获取当前的年、月和日。
这些信息在许多应用程序中都是必需的,例如日历应用、日期处理和数据分析等。
本文将介绍几种获取年月日的方法,帮助你在Java中轻松地处理日期。
1. 使用Java.util.Date类Java.util.Date类是Java中处理日期和时间的基本类之一。
要获取当前的年、月和日,可以使用以下代码:import java.util.Date;import java.text.SimpleDateFormat;public class Main {public static void main(String[] args) {Date date = new Date();SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");SimpleDateFormat sdfDay = new SimpleDateFormat("dd");String year = sdfYear.format(date);String month = sdfMonth.format(date);String day = sdfDay.format(date);System.out.println("Year: " + year);System.out.println("Month: " + month);System.out.println("Day: " + day);}}在上面的代码中,我们使用了SimpleDateFormat类来格式化日期。
通过传入不同的格式化模式,我们可以获取所需的日期部分。
使用Java创建简单的日历应用程序-一个实战教程日历应用程序是一个有用的工具,它允许用户记录和管理事件、约会和提醒。
在这个实战博客中,我们将创建一个Java日历应用程序,演示如何使用Java编程语言和图形用户界面(GUI)库来实现事件管理和日期选择功能。
以下是本实战博客的主要内容:项目概述准备工作创建Java项目设计GUI界面实现日历视图添加事件管理功能总结让我们开始吧!1. 项目概述在本项目中,我们将创建一个简单的Java日历应用程序,它包括以下主要功能:显示日历界面,允许用户选择日期。
在日历中标记已经安排的事件和约会。
允许用户添加、编辑和删除事件。
提供事件的日期和时间提醒功能。
我们将使用Java编程语言和Swing GUI库来构建这个日历应用程序。
2. 准备工作在开始之前,确保您的开发环境已设置好。
我们将使用Java编程语言和Swing库来构建日历应用程序,不需要额外的工具或库。
3. 创建Java项目首先,创建一个新的Java项目,您可以使用任何Java集成开发环境(IDE)来完成此操作。
在项目中,我们将创建Java类来实现日历应用程序。
4. 设计GUI界面我们将创建一个简单的Swing GUI界面,用于显示日历界面和事件列表。
创建一个Java类,例如CalendarApp,并在其中创建GUI界面。
javaCopy codeimport javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.Date;public class CalendarApp {private JFrame frame;private JPanel calendarPanel;private JPanel eventPanel;private JTextArea eventList;private JButton addButton;private JButton editButton;private JButton deleteButton;public CalendarApp() {frame = new JFrame("日历应用程序");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(800, 600);frame.setLayout(new BorderLayout());calendarPanel = new JPanel(new BorderLayout());eventPanel = new JPanel(new BorderLayout());eventList = new JTextArea();eventList.setEditable(false);JScrollPane eventScrollPane = new JScrollPane(eventList);addButton = new JButton("添加事件");editButton = new JButton("编辑事件");deleteButton = new JButton("删除事件");eventPanel.add(eventScrollPane, BorderLayout.CENTER);eventPanel.add(addButton, BorderLayout.NORTH);eventPanel.add(editButton, BorderLayout.WEST);eventPanel.add(deleteButton, BorderLayout.EAST);frame.add(calendarPanel, BorderLayout.WEST);frame.add(eventPanel, BorderLayout.CENTER);addButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {addEvent();}});editButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {editEvent();}});deleteButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {deleteEvent();}});frame.setVisible(true);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> new CalendarApp());}private void addEvent() {// 实现添加事件的代码,略...}private void editEvent() {// 实现编辑事件的代码,略...}private void deleteEvent() {// 实现删除事件的代码,略...}}在上述代码中,我们创建了一个CalendarApp类,包括一个Swing窗口、日历界面和事件列表。
Java获取法定节假日并解析1、接口调用:http://2、具体代码(红色部分为主要解析代码):/*** 日历导入根工具*/public class CalenderUtils {public static void main(String[] args) throws Exception {Prop p = e("jfinal_club_config_dev.txt");DruidPlugin druidPlugin = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());WallFilter wallFilter = new WallFilter();wallFilter.setDbType("mysql");druidPlugin.addFilter(wallFilter);druidPlugin.addFilter(new StatFilter());//me.add(druidPlugin);druidPlugin.start();ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin("mysql", druidPlugin);activeRecordPlugin.setContainerFactory(new CaseInsensitiveContainerFactory(true));_MappingKit.mapping(activeRecordPlugin);activeRecordPlugin.start();//arp.addMapping("c_workday", "id", Workday.class);List<Date> day2019 = getBetweenDates(new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01"), new SimpleDateFormat("yyyy-MM-dd").parse("2021-01-01"));for (Date day : day2019) {CloseableHttpClient httpCilent = HttpClients.createDefault();HttpGet httpGet = new HttpGet("/api/holiday/info/" + new SimpleDateFormat("yyyy-MM-dd").format(day));try {HttpResponse response = httpCilent.execute(httpGet);String line;JSONObject resultJsonObject;StringBuilder entityStringBuilder = new StringBuilder();BufferedReader b = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8 * 1024);while ((line = b.readLine()) != null) {entityStringBuilder.append(line);}//利用从HttpEntity中得到的String生成JsonObjectSystem.out.println(new SimpleDateFormat("yyyy-MM-dd").format(day) + "-->" + entityStringBuilder.toString());resultJsonObject = JSON.parseObject(entityStringBuilder.toString());Workday workday = new Workday();workday.setIsDelete(0);workday.setId(StringHELP.getUUID());workday.setDate(day);workday.setPublisher("1");workday.setPublisherName("admin");workday.setPublishDate(new Date());workday.setSubmitDate(new Date());workday.setStatus(0);workday.setStatuscashbag(1);//判断接口请求是否成功if(resultJsonObject.get("code").toString().equals("0")){//获取日期类型,进行相关逻辑处理//首先判断是否是节假日,是的话JSONObject objtype = JSON.parseObject(resultJsonObject.get("type").toString());System.out.println(objtype.get("type").toString());if(objtype.get("type").toString().equals("0")){workday.setType(1);}elseif(objtype.get("type").toString().equals("1")&&resultJsonObject.g et("holiday")==null){workday.setType(0);}else if(resultJsonObject.get("holiday")!=null){JSONObject objholiday = JSON.parseObject(resultJsonObject.get("holiday").toString());if(objholiday.get("holiday").toString().equals("true")){workday.setType(0);}else{workday.setType(1);}}}System.out.println("最终结果:"+workday.getType());//workday.setType(resultJsonObject.getInteger("data") == 0 || resultJsonObject.getInteger("data") == 2 ? 1 : 0);workday.setYear(day.getYear() + 1900);workday.setMonth(day.getMonth() + 1);workday.save();} catch (Exception e) {e.printStackTrace();} finally {try {httpCilent.close();//释放资源} catch (IOException e) {e.printStackTrace();}}}}/*** 获取两个日期之间的日期,包括开始结束日期* @param start 开始日期* @param end 结束日期* @return 日期集合*/private static List<Date> getBetweenDates(Date start, Date end) {List<Date> result = new ArrayList<>();Calendar tempStart = Calendar.getInstance();tempStart.setTime(start);tempStart.add(Calendar.DAY_OF_YEAR, 1);Calendar tempEnd = Calendar.getInstance();tempEnd.setTime(end);result.add(start);while (tempStart.before(tempEnd)) {result.add(tempStart.getTime());tempStart.add(Calendar.DAY_OF_YEAR, 1);}return result;}}。
日历源代码——java
//import java.sql.Date;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class GregorianCalendar日历 {
public static void main(String[] args) { //设置不同地区
Locale.setDefault();
//创建当前日历对象
GregorianCalendar now = new GregorianCalendar();
//从当前时期对象是取出时间日期对象
//编辑错误:Type mismatch: cannot convert from java.util.Date to java.sql.Date
Date date = now.getTime();
//将时间日期对象按字符形式打印
System.out.println(date.toString());
//重新将时间对象设置到日期对象中
now.setTime(date);
//从当前日期对象是取出当前月份、日期
int today =
now.get(Calendar.DAY_OF_MONTH);
int month = now.get(Calendar.MONTH);
//获取本月开始日期
now.set(Calendar.DAY_OF_MONTH, 1);
//获取本月开始日期在一周中的编号
int week = now.get(Calendar.DAY_OF_WEEK);
//打印日历头并换行设置当前月中第一天的开始位置
System.out.println("星期日星期一星期二
星期三星期四星期五星期六");
//设置当前月中第一天的开始位置
for( int i = Calendar.SUNDAY; i < week; i++){ System.out.print(" ");
//按规格打印当前月的日期数字
while(now.get(Calendar.MONTH) ==
month){
//取出当前日期
int day =
now.get(Calendar.DAY_OF_MONTH);
//设置日期数字小于10与不小于10两种情况的打
印规格
if(day < 10){
//设置当前日期的表现形式
if(day == today)
System.out.print(" <" + day + "> ");
else
System.out.print(" " + day + " ");
}
else{
//设置当前日期的表现形式
if(day == today)
System.out.print(" <" + day + "> ");
else
System.out.print(" " + day + " ");
}
//设置什么时候换行
if(week == Calendar.SATURDAY)
System.out.println();
//设置日期与星期几为下一天
now.add(Calendar.DAY_OF_MONTH, 1);
week = now.get(Calendar.DAY_OF_WEEK);
}
}
}
}。