DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date dt = new Date();
fxsetDate(dfparse(dfformat(dt)));
不过这样做有一个问题就是,即使前面将Date型格式化,存到实体类型里的Date仍然不带有格式,直接打印实体的Date型会默认直接调用Date类的toString方法,而Date类的toString方法默认格式是dow mon dd hh:mm:ss zzz yyyy,所以打出的格式会与你想存入的不符合,其实就相当于直接将dt存入了实体中。如果想取出带格式的除非再进行转化,否则得话还是建议把属性改成String型的。
int date, month;
Date time = new Date(SystemcurrentTimeMillis());
date = timegetMonth()+1;
month = timegetDate()+1;
Systemoutprintln(month+"/"+date);
那要看对方提供的是什么对象,如果是String类型的,那么就需要先转成Date对象,再转成Calendar对象,如果直接给的Date那么更简单,转成Calendar就可以了。
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
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 = sdfparse(d1);
Calendar c = CalendargetInstance();
csetTime(date);
Systemoutprintln(cget(CalendarMONTH));
}
}
这个月份是从0开始到11的,所以4月会输出3
/
获取指定月份的日历信息
@param year
年
@param month
月
@return
/
public static int[] getMonthCalendar(int year, int month) {
Calendar cl = CalendargetInstance();
clset(year, month, 1);
int firstDay = clgetMinimum(CalendarDAY_OF_MONTH);
int lastDay = clgetMaximum(CalendarDAY_OF_MONTH);
int[] day = new int[lastDay];
for (int i = 0; i < lastDay; i++) {
day[i] = i + firstDay;
}
return day;
}
package util;
import javamathBigDecimal;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
/
获取系统时间
/
public class DateUtil {
/ 日志对象 /
// private static Logger logger = LoggergetLogger(SystemUtilclass);
/ 获取年份 /
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 timeformatformat(date);
}
/
返回当前系统时间的年月日
@return
/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformatformat(date);
}
/
根据参数格式,格式化当前日期
@param format
@return
/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(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 = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarYEAR);
} 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 = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarMONTH)+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 = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarDAY_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 = ddparse(dateStr);
} catch (ParseException e) {
eprintStackTrace();
}
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 = timeformatparse(date);
long temp = dgetTime() - d1getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = bsetScale(2, BigDecimalROUND_HALF_UP)doubleValue();
return f1;
} catch (Exception e) {
eprintStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
Systemoutprintln(aa);
} catch (Exception e) {
eprintStackTrace();
}
}
}
可以通过日期的add(CalendarMONTH, 1)方法进行月份切换,输出每个满足条件的值
Date d1 = new SimpleDateFormat("yyyy-MM")parse("2015-6");//定义起始日期
Date d2 = new SimpleDateFormat("yyyy-MM")parse("2016-5");//定义结束日期
Calendar dd = CalendargetInstance();//定义日期实例
ddsetTime(d1);//设置日期起始时间
while(ddgetTime()before(d2)){//判断是否到结束日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String str = sdfformat(ddgetTime());
Systemoutprintln(str);//输出日期结果
ddadd(CalendarMONTH, 1);//进行当前日期月份加1
}
结果:
可以使用javautilCalendar来取得相关日期信息,下面给个示例供参考:
Java代码:
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
public class DateDemo {
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
String date = "2011-04-25 22:28:30";
Systemoutprintln(date);
Systemoutprintln("年份:" + getYear(date));
Systemoutprintln("月份:" + getMonth(date));
Systemoutprintln("日期:" + getDay(date));
Systemoutprintln("月初日期是: " + getMinMonthDate(date));
Systemoutprintln("月末日期是: " + getMaxMonthDate(date));
}
/
获取日期年份
@param date
@return
@throws ParseException
/
public static int getYear(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarYEAR);
}
/
获取日期月份
@param date
@return
@throws ParseException
/
public static int getMonth(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return (calendarget(CalendarMONTH) + 1);
}
/
获取日期号
@param date
@return
@throws ParseException
/
public static int getDay(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarDAY_OF_MONTH);
}
/
获取月份起始日期
@param date
@return
@throws ParseException
/
public static String getMinMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMinimum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
/
获取月份最后日期
@param date
@return
@throws ParseException
/
public static String getMaxMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMaximum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
}
以上就是关于在java中如何获得当前年份月份时间全部的内容,包括:在java中如何获得当前年份月份时间、JAVA获取当前月和日(急!!!在线等)、java 如何解析年月日,比如别人给我传一个年月日,我想单独获取月份怎么解析谢谢高手!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)