「java获得指定日期」java指定日期减一天工具类
本篇文章给大家谈谈java获得指定日期,以及java指定日期减一天工具类对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java 获取数据库中指定格式的日期
- 2、java如何通过传入一个指定日期获取该日期所
- 3、java如何获取当前时间 年月日 时分秒
- 4、java如何获取某一天的日期?
- 5、java 获取当前日期,应该如何操作呢
- 6、如果在JAVA中获得指定时间
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如何获取当前时间以及格式化需要用到两个类,如下图:
1.获取当前时间,并格式化为(年-月-日 时:分:秒)。
Date t = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(t));
打印输出结果如下图:
2.将java.util.Date转换为java.sql.Date格式。
java.sql.Date sqld = new java.sql.Date(t.getTime());
System.out.println(sqld);
java.sql.Time sqlt = new java.sql.Time(t.getTime());
System.out.println(sqlt);
java.sql.Timestamp sqlts = new java.sql.Timestamp(t.getTime());
System.out.println(sqlts);
打印输出结果如下图:
“拓展资料——java”:
Java是一种广泛使用的计算机编程语言,拥有跨平台、面向对象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。
Java编程语言的风格十分接近C++语言。继承了C++语言面向对象技术的核心,舍弃了容易引起错误的指针,以引用取代;移除了C++中的运算符重载和多重继承特性,用接口取代;增加垃圾回收器功能。
Java编程语言是个简单、面向对象、分布式、解释性、健壮、安全与系统无关、可移植、高性能、多线程和动态的语言。
java如何获取某一天的日期?
import java.util.*;
import java.text.*;
//1.由用户输入日期
String input = "2008-10-12"; //这个客户端输入
//把用户输入的日期转成 java 日期类
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date d = df.parse(input);
//输出结果
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int day = cal.get(DAY_OF_MONTH); //日
int month = cal.get(MONTH) + 1; //月(从0开始, 一般加1,实际是否 Calendar 里面常量的值决定的)
int year = cal.get(YEAR ); //年
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 Club 108092625
获取指定日期建议使用Calendar ,通过Calendar的add方法你可以设置获取当前日期前多少天,后多少天
比如使用下面的工具类:
String currentData= DateTimeUtils.getSystemDate() // 返回当前日期,格式为yyyy-MM-dd
String beforeFiveDays = DateTimeUtils.addDays(currentData, -5); //前五天
String afterFiveDays = DateTimeUtils.addDays(currentData, 5); //后五天
附上一个工具类:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期时间工具类
*/
public final class DateTimeUtil
{
private DateTimeUtil()
{
}
private static String DATE_FORMAT_PATTERN = "yyyyMMdd";
private static String TIME_FORMAT_PATTERN = "HHmmss";
/**
* 转换字符串为日期
*
* @param source
* 字符串形式的日期表示
* @return Date
*/
public static Date toDateTime(String source, String pattern)
{
Date date = null;
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
date = dateFormat.parse(source);
}
catch (ParseException e)
{
// nothing
}
return date;
}
/**
* 转换字符串为日期
*
* @param source
* 字符串形式的日期codeyyyyMMdd/code
* @return Date
*/
public static Date toDate(String source)
{
return toDateTime(source, DATE_FORMAT_PATTERN);
}
/**
* 转换字符串为时间
*
* @param source
* 字符串形式的时间codeHHmmss/code
* @return Date
*/
public static Date toTime(String source)
{
return toDateTime(source, TIME_FORMAT_PATTERN);
}
/**
* 将一种形式的字符串日期转换为另一种形式的字符串日期
*
* @param source
* 原日期字符串
* @param fromPattern
* 原日期字符串格式
* @param toPattern
* 目标日期字符串格式
* @return 新的日期字符串
*/
public static String convert(String source, String fromPattern, String toPattern)
{
Date date = toDateTime(source, fromPattern);
if (date == null)
{
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(toPattern);
return dateFormat.format(date);
}
/**
* 在指定的日期上面增加指定的天数
*
* @param source
* 源日期(yyyyMMdd)
* @param days
* 天数,正负皆可
* @return
*/
public static String addDays(String source, int days)
{
Date date = toDate(source);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, days);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return dateFormat.format(calendar.getTime());
}
/**
* 在指定的日期上面增加指定的月数
*
* @param source
* 原日期(yyyyMMdd)
* @param months
* 月数,正负皆可
* @return
*/
public static String addMonth(String source , int months)
{
Date date = toDate(source);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, months);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return dateFormat.format(calendar.getTime());
}
/**
* 在指定的日期上面增加指定的年数
*
* @param source
* 原日期(yyyyMMdd)
* @param years
* 年数,正负皆可
* @return
*/
public static String addYears(String source, int years)
{
Date date = toDate(source);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, years);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return dateFormat.format(calendar.getTime());
}
/**
* 返回指定格式的时间字符串
*
* @param format
* (返回字符串的格式)
* @return dateStr
* @throws ParseException
*/
public static String getSystemDateTime(String format)
{
Date date = new Date();
SimpleDateFormat simpDate = new SimpleDateFormat(format);
String dateStr = simpDate.format(date);
return dateStr;
}
/**
* 取当前日期,格式yyyyMMdd
*
* @return
*/
public static String getSystemDate()
{
return getSystemDateTime(DATE_FORMAT_PATTERN);
}
/**
* 取当前时间,格式HHmmss
*
* @return
*/
public static String getSystemTime()
{
return getSystemDateTime(TIME_FORMAT_PATTERN);
}
/**
* 格式化指定日期
*
* @param date
* 日期
* @param pattern
* 格式串
* @return
*/
public static String format(Date date, String pattern)
{
SimpleDateFormat simpDate = new SimpleDateFormat(pattern);
String dateStr = simpDate.format(date);
return dateStr;
}
/**
* 格式化指定日期
*
* @param date
* 日期
* @param pattern
* 格式串
* @return
*/
public static String format(long date, String pattern)
{
Date date2 = new Date(date);
return format(date2, pattern);
}
}
java获得指定日期的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java指定日期减一天工具类、java获得指定日期的信息别忘了在本站进行查找喔。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。