「java获取当前月日」java获取当前日期年月日
本篇文章给大家谈谈java获取当前月日,以及java获取当前日期年月日对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
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如何得到年月日。
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));
扩展资料
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怎么获得当前年月日并赋给变量?
Java获得当前年月日并赋值给变量,主要是使用java 提供的工具类Calendar,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());
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);
}
}
输出结果:
年: 2012
月: 1
日: 13
时: 17
分: 28
秒: 19
当前时间毫秒数:1326446899902
Fri Jan 13 17:28:19 CST 2012
Fri Jan 13 17:28:19 CST 2012
格式化后的日期:2012-01-13 17:28:19
字符串转成日期:Fri Jan 13 17:26:33 CST 2012
关于java获取当前月日和java获取当前日期年月日的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-12,除非注明,否则均为
原创文章,转载请注明出处。