当前位置:首页 » 编程语言 » 年月日java

年月日java

发布时间: 2023-05-21 18:08:21

java如何得到年月日。

1、获取当前的时间

Date date=new Date();//此时date为当前的时间

2、设置时间的格式

Date date=new Date();//此时date为当前的时间

System.out.println(date);

SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//设置当前时间的格式,为年-月-日

System.out.println(dateFormat.format(date));

SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”);//设置当前时间的格式,为年-月-日 时-分-秒

System.out.println(dateFormat_min.format(date));

(1)年月日java扩展阅读

java 获取当前微秒时间:

package com.ffcs.itm;

public class DataSecUtils {

public static void main(String[] args) {

System.out.println(System.currentTimeMillis()); // 毫秒

System.out.println(getmicTime());

System.out.println(System.currentTimeMillis()); // 毫秒

System.out.println(getmicTime());

}

/**

* @return返回微秒

*/

public static Long getmicTime() {

Long cutime = System.currentTimeMillis() * 1000; // 微秒

Long nanoTime = System.nanoTime(); // 纳秒

return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;

}

}

㈡ JAVA 如何单独取得"年","月","日"...

//CalendarTest.java
//Calendar对象能很好解决你的问题
import java.util.Calendar;
public class CalendarTest {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int y=c.get(Calendar.YEAR);//年
int M = c.get(Calendar.MONTH)+1;//月,注意这里要加1,计算机第一个月从0开始
int d = c.get(Calendar.DATE);//日
System.out.println("年:"+y);
System.out.println("月:"+M);
System.out.println("日:"+d);
String dateStr = "";
dateStr+=y+"-"+(M<10?"0":"")+M+"-"+(d<10?"0":"")+d;
System.out.println(dateStr);
}
}

㈢ java 如何解析年月日,比如别人给我传一个年月日,我想单独获取月份怎么解析谢谢高手!

那要看对方提供的肢老配是什么对象,如果是String类型的,那么就需要先转成Date对象,再转成Calendar对象,如果直接给的Date那么更简单,转成Calendar就可以了。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test12{
public static void main(String args[]) throws Exception{
String d1 = "2012-04-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(d1);
Calendar c = Calendar.getInstance();
c.setTime(date);
System.out.println(c.get(Calendar.MONTH));
}
}
这个月份是从0开历指始到11的,所以4月会输出含拦3

㈣ 将java怎么将long类型的时间转换成年月日的形式

用java代码实现:

public static String longToDate(long lo){

Date date = new Date(lo);

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return sd.format(date);

}

资料拓展:

长整型(long)是计算机程序语言,是程序设计中数据类型的一种表现方式,一般情况下用long 表示长整型。 long 有符号64位整数,范围是-2^63-2^63 -1 Int64。

㈤ (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月。

(5)年月日java扩展阅读

其他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(days>365){

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

}else{

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

}

}

}

输出的结果如下:

month=12

day=31

第365天

㈥ Java中如何设置Date对象的年月日

Date
public Date(int year,
int month,
int day)
参数:
year - year 减去 1900,它必须是 0 到 8099 之间的数。(注意,8099 是由 9999 减去 1900 得到的。)
month - 0 到 11 之间的数
day - 1 到 31 之间的数

测试代码如下:
import java.util.Date;

public class Test {
public static void main(String args[]){
Date date = new Date(2010-1900,1,10);
System.out.println(date);
}
}

运行结果:
Wed Feb 10 00:00:00 CST 2010

希望对你有帮助。。。。。。仍有问题可以HI我。。。。

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

java获取一个时间的年月日代码及相关解释说明参考下面代码
package;
importjava.util.Calendar;
publicclassTest{
publicstaticvoidmain(String[]args){
Calendarcal=Calendar.getInstance();//使用日历类
intyear=cal.get(Calendar.YEAR);//获取年份
intmonth=cal.get(Calendar.MONTH)+1;//获取月份,因为从0开始的,所以要加1
intday=cal.get(Calendar.DAY_OF_MONTH);//获取天
System.out.println("结果:"+year+"-"+month+"-"+day);
}
}

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

//得到long类型当前时间

longl=System.currentTimeMillis();

//new日期对

Datedate=newDate(l);

//转换提日期输出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

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

(8)年月日java扩展阅读

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());

Date d = new Date();

System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);

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

String str = "2012-1-13 17:26:33";

//要跟上面sdf定义的格式一样
Date today = sdf.parse(str);

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

㈨ java获得当前年月日

很多朋友都想知道java怎么获得当前年月日?下面就一起来了解一下吧~

两种方法,通过Date类或者通过Calendar类,Date类比较简单,但是要得到细致的字段的话Calendar类比较方便。
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import org.junit.Test; public class GetTimeNow { /** * 通过Calendar类获取 */ @Test public void getTimeNowThroughCalendar(){ //使用默认时区和语言环境获得一个日历。 Calendar    rightNow    =    Calendar.getInstance();    /*用Calendar的get(int field)方法返回给定日历字段的值。 HOUR 用于 12 小时制时钟 (0 - 11),HOUR_OF_DAY 用于 24 小时制时钟。*/ Integer 乎码year = rightNow.get(Calendar.YEAR);  Integer month = rightNow.get(Calendar.MONTH)+1; //第一个月从0开始,所以得到月份+1 Integer day = rightNow.get(rightNow.DAY_OF_MONTH); Integer hour12 = rightNow.get(rightNow.HOUR);  Integer hour24 = rightNow.get(rightNow.HOUR_OF_DAY); Integer minute = rightNow.get(rightNow.MINUTE); Integer second = rightNow.get(rightNow.SECOND); Integer millisecond = rightNow.get(rightNow.MILLISECOND); String 银族TimeNow12 = year+"-"+month+"-"+day+" "+hour12+":"+minute+":"+second+":"+millisecond; String TimeNow24 = year+"-"+month+"-"+day+" "+hour24+":"+minute+":"+second+":"+millisecond; System.out.println("日历:"+rightNow+"\n12小时制时钟:"+TimeNow12+"\n24小时制时钟:"+TimeNow24); } /** * 通过Date类获取 */ @Test public void getTimeNowThroughDate(){ Date date=new Date(); DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS"); System.out.println(format.format(date)); //DateFormat类的静态工厂方法 System.out.println(format.getInstance().format(date)); System.out.println(format.getDateInstance().format(date)); System.out.println(format.getTimeInstance().format(date)); System.out.println(format.getDateTimeInstance().format(date)); //DateFormat带参数的静态工厂方法 //第一个参数是锋顷弊静态变量style有4中取值0、1、2、3分别对应SHORT、MIDIUM、LONG、FULL //第二个参数根据环境敏感的Locale类的静态变量自定义输出 System.out.println(format.getDateInstance(0, Locale.CHINA).format(date));  System.out.println(format.getTimeInstance(0,Locale.CHINA).format(date)); System.out.println(format.getDateTimeInstance(2,2).format(date));   } /** * 两者结合。。。 */ @Test public void getTimeNowTogether(){ String TimeNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(Calendar.getInstance().getTime()); System.out.println(TimeNow); } }
 

日历: java.util.GregorianCalendar[time=1454251772565,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=6,WEEK_OF_MONTH=6,DAY_OF_MONTH=31,DAY_OF_YEAR=31,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=49,SECOND=32,MILLISECOND=565,ZONE_OFFSET=28800000,DST_OFFSET=0] 12小时制时钟:2016-1-31 10:49:32:565 24小时制时钟:2016-1-31 22:49:32:565 2016-01-31 22:49:50:36 16-1-31 下午10:49 2016-1-31 22:49:50 2016-1-31 22:49:50 2016年1月31日 星期日 下午10时49分50秒 CST 2016-1-31 22:49:50 2016-01-31 22:50:09:270

㈩ java中怎样输入年月日

import java.io.*;
public class Date{

public static void main(String [] args)throws NumberFormatException, IOException{
int year = 0;
int month = 0;
int day = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
year = Integer.parseInt(br.readLine());
month = Integer.parseInt(br.readLine());
day = Integer.parseInt(br.readLine());
System.out.println(year + "年" + month + "月" + day + "日");
}

}

说明 :
输入2008 回车
输入 5 回车
输入 30 回车

输出 2008年5月30日

热点内容
浏览器打不开服务器通信怎么办 发布:2024-05-18 21:32:22 浏览:961
创建存储空间 发布:2024-05-18 21:20:57 浏览:122
sql日期和时间 发布:2024-05-18 21:16:19 浏览:143
安卓网页怎么截取 发布:2024-05-18 20:53:56 浏览:972
在配置更新的时候没电关机怎么办 发布:2024-05-18 20:36:10 浏览:928
win7访问win2000 发布:2024-05-18 20:27:41 浏览:389
青岛人社局密码多少 发布:2024-05-18 20:19:10 浏览:735
无法存储呼叫转移 发布:2024-05-18 20:18:30 浏览:128
数据库的调优 发布:2024-05-18 20:18:29 浏览:347
sqlserver注册表清理 发布:2024-05-18 20:13:14 浏览:993