本文目录一览:
- 1、java怎么获得当前年月日并赋给变量?
- 2、java中从数据库中读出日期类型后怎么提取年月或者日?
- 3、java 获取当前日期,应该如何操作呢
- 4、java如何获取当前时间 年月日 时分秒
- 5、java 怎么获取一个时间的年月日
- 6、java怎么获取当前时间的年月日
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中从数据库中读出日期类型后怎么提取年月或者日?
直接通过格式转换的形式即可。举例:
String str0 = "2015年07月05日";
Date d1 = new SimpleDateFormat("yyyy年MM月dd日").parse(str0);//定义起始日期
SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy");
SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
SimpleDateFormat sdf2= new SimpleDateFormat("dd");
String str1 = sdf0.format(d1);
String str2 = sdf1.format(d1);
String str3 = sdf2.format(d1);
System.out.println("年份为:"+str1);
System.out.println("月份为:"+str2);
System.out.println("日为:"+str3);
结果:
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如何获取当前时间 年月日 时分秒
//得到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怎么获取当前时间的年月日
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
String str=sdf.format(date);
system.out.println(str);