「java获取指定日期」java获取指定日期的时间戳

博主:adminadmin 2022-12-28 07:18:06 55

今天给各位分享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 获取数据库中指定格式的日期

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class TestDate {

public static void main(String[] args) {

String str1 = "Mon Mar 05 00:00:00 CST 2012";

String modle="yyyy-MM-dd HH:mm:ss";

System.out.println(getTime(str1,modle));

}

public static String getTime(String str,String model){

String dateStr="";

SimpleDateFormat f=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.ENGLISH);

try {

Date date=f.parse(str);

dateStr=new SimpleDateFormat(model).format(date);

} catch (Exception e) {

e.printStackTrace();

}

return dateStr;

}

}

java如何通过传入一个指定日期获取该日期所

Calendar c = Calendar.getInstance();

c.setTimeInMillis(date.getTime());

c.add(Calendar.DATE, amount);

str.formatDate(date4, "yyyy-MM-dd");

//date.getTime() 当前日期

//amount 传入的N天数

例如:

SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); //字符串转换

Calendar c = Calendar.getInstance();

//new Date().getTime();这个是获得当前电脑的时间,你也可以换成一个随意的时间

c.setTimeInMillis(new Date().getTime());

c.add(Calendar.DATE, 5);//天后的日期

Date date= new Date(c.getTimeInMillis()); //将c转换成Date

System.out.println("date="+formatDate.format(date4));

关于java获取指定日期和java获取指定日期的时间戳的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。