日历源代码——java

合集下载

java获取当前时间和前一天日期(实现代码)

java获取当前时间和前一天日期(实现代码)
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式 String defaultStartDate = sdf.format(dBefore); //格式化前一天 String defaultEndDate = sdf.format(dNow); //格式化当前时间
这篇文章主要给大家介绍了关于java中optional使用的相关资料文中通过示例代码介绍的非常详细对大家的学习或者工作具有一定的参考学习价值需要的朋友们下面随着小编来一起学习学习吧
java获 取 当 前 时 间 和 前 一 天 日 期 ( 实 现 如下:
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; Date dNow = new Date(); //当前时间 Date dBefore = new Date(); Calendar calendar = Calendar.getInstance(); //得到日历 calendar.setTime(dNow);//把当前时间赋给日历 calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天 dBefore = calendar.getTime(); //得到前一天的时间
System.out.println("前一天的时间是:" + defaultStartDate); System.out.println("生成的时间是:" + defaultEndDate);

简易日历代码

简易日历代码

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。

JAVA处理日期时间常用方法

JAVA处理日期时间常用方法

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,日,时,分,秒。

JavaDate实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1

JavaDate实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1
String类型 2018-01-23 String类型转Date类型 Tue Jan 23 00:00:00 CST 2018 Date结束日期: 2018-01-23 Dat2018-01-24
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date sDate = sdf.parse(endDate); System.out.println("String类型转Date类型 "+sDate); //要实现日期+1 需要String转成Date类型
Format f = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("Date结束日期:" + f.format(sDate));
Calendar c = Calendar.getInstance(); c.setTime(sDate); c.add(Calendar.DAY_OF_MONTH, 1);
//利用Calendar 实现 Date日期+1天
sDate = c.getTime(); System.out.println("Date结束日期+1 " +f.format(sDate)); //打印Date日期,显示成功+1天
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); endDate = sdf1.format(sDate); System.out.println("Date类型转String类型 "+endDate); //将日期转成String类型 方便进入数据库比较

java编程日历代码

java编程日历代码
return formatter.format(cal.getTime());
}
/**
* 格式化货币,将小写转成中文大些
* @param money
* @return
*/
public static String getCapMoney(String money) {
if(decimals[0]=='0'){
if(decimals[1]=='0'){
resule = resule + "整";
}else{
resule = resule + "零" +
Chinamemoney(Integer.parseInt(String.valueOf(decimals[1]))) +
/**
* 格式化成货币类型,即四舍五入保留小数点两位,没有就补零
* @param money
* @return
*/
public static String formatcurrency(String money){
try {
if (money == null || "".equals(money)) {
}
//取得当前时间
public static String getDateTime() {
Date d = new Date();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
resule = Chinamemoneyunit(j,

java获取年月日的方法

java获取年月日的方法

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万年历源代码(可运行)
private JTable table = new JTable(dtm); //装日期的表格
private JScrollPane sp = new JScrollPane(table);
private JButton bLastYear = new JButton("上一年");
p3.add(jsp, BorderLayout.CENTER);
p3.add(p2, BorderLayout.SOUTH);
p3.add(ld, BorderLayout.NORTH);
private JTextArea jta = new JTextArea(); //jta--JTextArea
private JScrollPane jsp = new JScrollPane(jta);
private JLabel l = new JLabel("年份文本框中可直接键入要查找的年份,以提高查询效率");
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import java.sql.Date;
cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1])-1, 0);
showCalendar(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]), cld);

java获取年月日的方法

java获取年月日的方法

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类来格式化日期。

通过传入不同的格式化模式,我们可以获取所需的日期部分。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

日历源代码——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);
}
}
}
}。

相关文档
最新文档