「java得到系统的当前日」java中怎么获取当前系统的日期
本篇文章给大家谈谈java得到系统的当前日,以及java中怎么获取当前系统的日期对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
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中如何输出当前系统日期?
利用java里的Date类输出,进阶的做法还可以用simpleDateformat类进行格式化输出日期。代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日期格式化
* @author young
*
*/
public class SimpleDateFormatTest {
public static void main(String[] args) {
// 在构造器中传入日期样式
// SimpleDateFormat sdf=new SimpleDateFormat(
// "yyyy.MM.dd G 'at' HH:mm:ss z");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
// 当前系统时间
Date date = new Date();
// 调用format(Date date)对象传入的日期参数进行格式化
// format(Date date)将日期转化成字符串
String formatDate = sdf.format(date);
System.out.println("格式化后的日期为:" + formatDate);
}
}
java 获取当前日期,应该如何操作呢
package util;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 获取系统时间
*
*/
public class DateUtil {
/* 日志对象 */
// private static Logger logger = Logger.getLogger(SystemUtil.class);
/* 获取年份 */
public static final int YEAR = 1;
/* 获取年月 */
public static final int YEARMONTH = 2;
/* 获取年月日 */
public static final int YEARMONTHDAY = 3;
/* 获取年月日,小时 */
public static final int YMD_HOUR = 4;
/* 获取年月日,小时,分钟 */
public static final int YMD_HOURMINUTE = 5;
/* 获取年月日,时分秒 */
public static final int FULL = 6;
/* 获取年月日时分秒 格式:yyyyMMddHHmmss */
public static final int UTILTIME = 7;
/**
* 根据指定时间格式类型得到当前时间
*
* @param type
* 时间类型
* @return String 字符串时间
*/
public static synchronized String getCurrentTime(int type) {
String format = getFormat(type);
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformat.format(date);
}
/**
* 返回当前系统时间的年月日
*
* @return
*/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformat.format(date);
}
/**
* 根据参数格式,格式化当前日期
* @param format
* @return
*/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformat.format(date);
}
/**
* 根据指定时间格式类型,格式化时间格式
*
* @param type
* 时间格式类型
* @return
*/
private static String getFormat(int type) {
String format = "";
if (type == 1) {
format = "yyyy";
} else if (type == 2) {
format = "yyyy-MM";
} else if (type == 3) {
format = "yyyy-MM-dd";
} else if (type == 4) {
format = "yyyy-MM-dd HH";
} else if (type == 5) {
format = "yyyy-MM-dd HH:mm";
} else if (type == 6) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 7) {
format = "yyyyMMddHHmmss";
} else {
throw new RuntimeException("日期格式参数错误");
}
return format;
}
public static int getYear(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getMonth(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH)+1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getDay(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Date StringToDate(String dateStr, String formatStr) {
SimpleDateFormat dd = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = dd.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static double getHours(String date) {
SimpleDateFormat timeformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date d = new Date();
Date d1 = timeformat.parse(date);
long temp = d.getTime() - d1.getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
System.out.println(aa);
} catch (Exception e) {
e.printStackTrace();
}
}
}
关于java得到系统的当前日和java中怎么获取当前系统的日期的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。