「年月日java」年月日java格式

博主:adminadmin 2023-03-22 07:17:13 529

本篇文章给大家谈谈年月日java,以及年月日java格式对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java 中年月日的问题

如果楼主用作应用开发..可以考虑java类库中的Date或者Calendar

如果只是研究算法可以参考下列代码

按楼主意图编写了以下代码,用MyDate存放日期.

class MyDate {

int year;

int month;

int day;

// 非闰年每月的天数

int[] days4MonthsOfNLY = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// 闰年每月的天数

int[] days4MonthsOfLY = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// 构造函数

MyDate(int year, int month, int day) {

this.year = year;

this.month = month;

this.day = day;

}

// 在当前日期上添加days天,返回得到的日期

MyDate addDate(int days) {

int[] days4Months = null;

if (isLeapYear(this.year)) {

days4Months = days4MonthsOfLY;

} else {

days4Months = days4MonthsOfNLY;

}

// 用于构建新对象

int newYear = year, newMonth = month, newDay = day;

if (day + days = days4Months[month - 1]) {

// 如果n天后仍在本月

newDay = day + days;

} else {

// 如果n天后本月已经过完

days = days - (days4Months[month - 1] - day);

newMonth++;

if (newMonth 12) {

newMonth = 1;

if (isLeapYear(++newYear)) {

days4Months = days4MonthsOfLY;

} else {

days4Months = days4MonthsOfNLY;

}

}

while (days - days4Months[newMonth - 1] 0) {

days = days - days4Months[newMonth - 1];

newMonth++;

if (newMonth 12) {

newMonth = 1;

if (isLeapYear(++newYear)) {

days4Months = days4MonthsOfLY;

} else {

days4Months = days4MonthsOfNLY;

}

}

}

newDay = days;

}

return new MyDate(newYear, newMonth, newDay);

}

// 判断闰年

static boolean isLeapYear(int year) {

if (year % 4 == 0 year % 100 != 0 || year % 400 == 0) {

System.err.println("润:" + year);

return true;

} else

return false;

}

// 覆写toString方法

@Override

public String toString() {

return "[" + year + "-" + month + "-" + day + "]";

}

}

public class DateAnalyzer {

public static void main(String[] args) {

// 用于测试程序

MyDate date = new MyDate(1980, 2, 28);

System.err.println(date.addDate(32));

}

}

java如何得到年月日。

package test;

import java.util.Calendar;

public class Test {

public static void main(String[] args) {

Calendar cal=Calendar.getInstance();//使用日历类

int year=cal.get(Calendar.YEAR);//得到年

int month=cal.get(Calendar.MONTH)+1;//得到月,因为从0开始的,所以要加1

int day=cal.get(Calendar.DAY_OF_MONTH);//得到天

int hour=cal.get(Calendar.HOUR);//得到小时

int minute=cal.get(Calendar.MINUTE);//得到分钟

int second=cal.get(Calendar.SECOND);//得到秒

System.out.println("结果:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);

}

}

(JAVA)输入年月日,计算日期是今年的第几天?

import java.util.Scanner;

/**

* Created by xpf on 2018/6/22 :)

* GitHub:xinpengfei520

* Function:

*/

public class CalculateUtils {

/*平年二月28天*/

private static final int DAYS_28 = 28;

/*闰年二月29天*/

private static final int DAYS_29 = 29;

/*除了31天的月份其他均为30天*/

private static final int DAYS_30 = 30;

/*1、3、5、7、8、10、12月份31天*/

private static final int DAYS_31 = 31;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Please input year:");

int year = input.nextInt();

System.out.println("Please input month:");

int month = input.nextInt();

System.out.println("Please input day:");

int day = input.nextInt();

int daysInYear = getDaysInYear(year, month, day);

System.out.println("daysInYear:" + daysInYear);

}

/**

* get days in this year

*

* @param year

* @param month

* @param day

* @return

*/

public static int getDaysInYear(int year, int month, int day) {

int totalDays = 0;

switch (month) {

// 12 月份加的是11月份的天数,依次类推

case 12:

totalDays += DAYS_30;

case 11:

totalDays += DAYS_31;

case 10:

totalDays += DAYS_30;

case 9:

totalDays += DAYS_31;

case 8:

totalDays += DAYS_31;

case 7:

totalDays += DAYS_30;

case 6:

totalDays += DAYS_31;

case 5:

totalDays += DAYS_30;

case 4:

totalDays += DAYS_31;

case 3:

// 判断是否是闰年

if (((year / 4 == 0) (year / 100 != 0)) || (year / 400 == 0)) {

totalDays += DAYS_29;

} else {

totalDays += DAYS_28;

}

case 2:

totalDays += DAYS_31;

case 1: // 如果是1月份就加上输入的天数

totalDays += day;

}

return totalDays;

}

}

【解题思路】

1、通过年份区分是闰年还是平年,平年 2 月 28 年,闰年 2 月 29 天。

2、1、3、5、7、8、10、12 月份为 31 天,其余月份为 30 天。

3、将每个月的天数相加即可,如果输入的是 12 月,则从 11 月往前累加到1月。

扩展资料

其他java计算日期的方式

package study01;

import java.util.Scanner;

public class TestDay {

/*

* 输入2017年的月和日:month=?,day=? 输出输入的日期是2017年的第几天,使用switch完成

*/

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("month=");

int month = sc.nextInt();

System.out.print("day=");

int day = sc.nextInt();

int days = 0;

switch (month) {

case 12:

days += 30;

case 11:

days += 31;     

case 10:

days += 30;

case 9:

days += 31;

case 8:

days += 31;

case 7:

days += 30;

case 6:

days += 31; 

case 5:

days += 30;       

case 4:

days += 31;

case 3:

days += 28;

case 2:

days += 31;

case 1:

days += day;               

}

if(days365){

System.out.println("你输入的已经超过了365天了");

}else{

System.out.println("第" + days + "天");

}

}

}

输出的结果如下:

month=12

day=31

第365天

参考资料:百度百科-Scanner

参考资料:百度百科-java

java如何获取当前时间 年月日 时分秒

//得到long类型当前时间

long l = System.currentTimeMillis();

//new日期对

Date date = new Date(l);

//转换提日期输出格式

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-

dd HH:mm:ss");System.out.println(dateFormat.format(date));

扩展资料

package com.ob;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " + now.get(Calendar.YEAR));

System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));

System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " + now.get(Calendar.MINUTE));

System.out.println("秒: " + now.get(Calendar.SECOND));

System.out.println("当前时间毫秒数:" + now.getTimeInMillis());

System.out.println(now.getTime());

\t\tDate d = new Date();

System.out.println(d);

\t\tSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

\t\tString dateNowStr = sdf.format(d);

System.out.println("格式化后的日期:" + dateNowStr);

\t\t

\t\tString str = "2012-1-13 17:26:33";

//要跟上面sdf定义的格式一样

\t\tDate today = sdf.parse(str);

System.out.println("字符串转成日期:" + today);

\t}

}

参考资料:Java - 百度百科

java 怎么获取一个时间的年月日

java获取一个时间的年月日代码及相关解释说明参考下面代码

package zhidao;

import java.util.Calendar;

public class Test {

 public static void main(String[] args) {

  Calendar cal=Calendar.getInstance();//使用日历类

  int year=cal.get(Calendar.YEAR);//获取年份

  int month=cal.get(Calendar.MONTH)+1;//获取月份,因为从0开始的,所以要加1

  int day=cal.get(Calendar.DAY_OF_MONTH);//获取天

  System.out.println("结果:"+year+"-"+month+"-"+day);

 }

}

java中的年月日简单编程

将这段代码拷贝到UseBirthday类中和你的比较一下看看

public class UseBirthday {

public static void main(String[] args) {

Birthday a=new Birthday(2000,1,1);

a.GetBirthday();

a.Birthday(2008,5,12);

a.GetBirthday();

}

}

class Birthday {

private int Year, Month, Day;

Birthday(){

Year=2000;

Month=1;

Day=1;

}

Birthday(int y,int m,int d) {

Year = y;

Month = m;

Day = d;

}

public void Birthday(int y,int m,int d){

Year = y;

Month = m;

Day = d;

}

int Gety() {

return Year;

}

int Getm() {

return Month;

}

int Getd() {

return Day;

}

void GetBirthday() {

System.out.println(Gety() + "年" + Getm() + "月" + Getd() + "日");

}

}

关于年月日java和年月日java格式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。