java获取当前时间几天月年前的时间方法

java获取当前时间几天月年前的时间方法,第1张

public static void main(String[] args) {

Calendar calendar = CalendargetInstance(LocalegetDefault());

Systemoutprintln(calendarget(CalendarYEAR));

Systemoutprintln(calendarget(CalendarMONTH)+1);

Systemoutprintln(calendarget(CalendarDATE));

}

3行代码分别用于获取当前时间的年、月、日,获取月份的时候需要+1,因为月份取的索引值,从0-11

是否可以拦截对Systemoutprint 和Systemerrprint (在Java中)的调用并为它们加上时间戳? 不用担心,我们使用常规的日志记录框架,但是偶尔会有一些sysout泄漏出去,所以很高兴知道它何时发生,因此我们可以将其绑定到正确的日志文件。

您可以为此使用方面。 查看AspectJ。

你能行的。

查看文件

Reassigns the"standard" output stream

First, if there is a security manager, its checkPermission method is called with a RuntimePermission("setIO") permission to see if it's ok to reassign the"standard" output stream

public class CustomPrintStream extends PrinterStream{

//override print methods here

}

SystemsetOut(new CustomPrintStream());

您将必须重写PrintStream中所有可能的print()和println()实现,并根据需要重新格式化输出。

@maclema正是我忘了在那发表评论。

您可以使用面向方面的编程来实现这一点-特别是AspectJ工具。

这个想法是您定义与代码中的点匹配的切入点,然后编写在这些点执行的建议。然后,AspectJ编译器会在这些时候编织您的建议。

因此,对于您的问题,您首先需要定义一个切入点,该切入点每次在PrintStream上调用print方法时都会拾取

pointcut callPrint(PrintStream ps, String s) :

call( javaioPrintStreamprint()) && target(ps) && args(s);

然后,如果PrintStream是Systemout,您将编写一些建议以替换该参数(这与Systemerr相同)。

void around(PrintStream ps, String s) : callPrint(ps,s) {

if(psequals(Systemout)){

String new_string =

proceed(new_string);

}

else proceed(s);

}

然后,您需要将所有这些都放在一个方面并将其编织到您的代码中-在线上有很多关于如何做到这一点的教程。

应该有可能。

Systemout是一个printStream。

您可以扩展流以将日期和时间附加到打印方法,并使用SystemsetOut()适当地设置流。

事后,如果您想确定打印语句的来源,可以使用:

ThreadcurrentThread()getStackTrace()[1]getClassName();

Date date=new Date();

DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String time=formatformat(date);

不同的方法介绍如下:

1、通过Date类来获取当前时间。

Date day=new Date()

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

Systemoutprintln(dfformat(day))

2、通过System类中的currentTimeMillis方法来获取当前时间。

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   Systemoutprintln(dfformat(SystemcurrentTimeMillis()))

3、通过Calendar类来获取当前时间。

Calendar c = CalendargetInstance();//可以对每个时间域单独修改

int year = cget(CalendarYEAR)

int month = cget(CalendarMONTH)

int date = cget(CalendarDATE)

int hour = cget(CalendarHOUR_OF_DAY)

int minute = cget(CalendarMINUTE)

int second = cget(CalendarSECOND)

Systemoutprintln(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second)

4、通过Date类来获取当前时间。

Date date = new Date()

String year = Stringformat("%tY", date)

String month = Stringformat("%tB", date)

String day = Stringformat("%te", date)

Systemoutprintln("今天是:"+year+"-"+month+"-"+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();

}

}

}

calendar 当前时间加一天怎么做? java, java中的calendar如何在当前时间加一天?

方法如下:

Date date = new Date();设定当前日期

calendaradd(CalendarsetTime(date);/日历物件

calendar;当前时间

Calendar calendar = CalendargetInstance();/DAY_OF_MONTH, 1);天数加一

Java是一种可以撰写跨平台应用程式的面向物件的程式设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、资料中心、游戏控制台、科学超级计算机、行动电话和网际网路,同时拥有全球最大的开发者专业社群。

与传统程式不同,Sun 公司在推出 Java 之际就将其作为一种开放的技术。全球数以万计的 Java 开发公司被要求所设计的 Java 软体必须相互相容。“Java 语言靠群体的力量而非公司的力量”是 Sun 公司的口号之一,并获得了广大软体开发商的认同。这与微软公司所倡导的注重精英和封闭式的模式完全不同。

Sun 公司对 Java 程式语言的解释是:Java 程式语言是个简单、面向物件、分散式、解释性、健壮、安全与系统无关、可移植、高效能、多执行绪和动态的语言。

Java 平台是基于 Java 语言的平台。这样的平台目前非常流行,因此微软公司推出了与之竞争的NET平台以及模仿 Java 的 C#语言。

java怎么获得当前时间多一天

java在当前系统时间加一天主要是使用calendar类的add方法,如下程式码:

import javautilCalendar;

import javautilDate;

public class ceshi {

public static void main(String[] args) {

Date date = new Date(); 新建此时的的系统时间

Systemoutprintln(getNextDay(date)); 返回明天的时间

}

public static Date getNextDay(Date date) {

Calendar calendar = CalendargetInstance();

calendarsetTime(date);

calendaradd(CalendarDAY_OF_MONTH, +1);+1今天的时间加一天

date = calendargetTime();

return date;

}

}

php怎么将当前时间戳增加一year

$now_date = time(); 获得当前时间戳

$year = date("Y",$date); 得到当前 year

$o_date = date("-m-d G:i:s",$date); 除了year 外的日期字串

$result = strtotime(($year+1)$o_date); year + 1 然后以字串连结的形式和$o_date结合成日期字串,再strtotime转化时间戳

----------------------------------------------

上述是考虑到闰year会多1天。

如果不需要考虑闰year 。

可以直接加上1year(平year)的秒数~

也就是360024365

----------------------------------------------

year 居然是 “不适合” 词汇

WHY??

echo strtotime("+1 year"); 返回的是时间戳, 如果要转换成一般时间格式还需要下面的函式

echo date('Y-m-d H:i:s', strtotime("+1 year"));

==================================================================

同理,不仅仅可以+year 还可以是天, 月日都可以的,如下程式码:

<php

echo strtotime("now"), "\n";

echo strtotime("10 September 2000"), "\n";

echo strtotime("+1 day"), "\n";

echo strtotime("+1 week"), "\n";

echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";

echo strtotime("next Thursday"), "\n";

echo strtotime("last Monday"), "\n";

>

Java取当前时间

tomcat时间跟系统时间不一致的问题解决方法 摘自 -- 黑夜的部落格 一,在catalinabat中 配置如下: set JAVA_OPTS=%JAVA_OPTS% -Dusertimezone=GMT+08 -Xms256m -Xmx800m -Djavautilloggingmanager=apachejuliClassLoaderLogManager -Djavautilloggingconfigfile="%CATALINA_BASE%\conf\loggingproperties" -Xms256m -Xmx800m(初始化记忆体大小为256m,可以使用的最大记忆体为800m), -Dusertimezone=GMT+08 设定为北京时间 二,在eclipse中设定 在 首选项->Tomcat ->JVM Settings 项,设定JRE的版本为'jre150_06',并且新增如下几个JVM Parameters: -Xms128m -Xmx512m -Dfileencoding=UTF8 -Dusertimezone=GMT+08

java中怎么获取当前时间的前一天

public static Date getNextDay(Date date) {

Calendar calendar = CalendargetInstance();

calendarsetTime(date);

calendaradd(CalendarDAY_OF_MONTH, -1);

date = calendargetTime();

return date;

}

Java怎么系统时间减当前时间

系统时间、当前时间,如果都是同时区,结果是0

~~~~~~~~

java怎么获取当前时间

/

获取系统当前时间 <br>

方 法 名:getCurrentDate<br>

@param formatStr

需要格式的目标字串例:yyyy-MM-dd

@return Date 时间物件

/

publicstatic Date getCurrentDate() {

returnnew Date();

}

publicString getTodayString() {

Calendarca = CalendargetInstance();

StringcurrDate = caget(CalendarYEAR) + "-"

+(caget(CalendarMONTH) + 1) + "-"

+caget(CalendarDAY_OF_MONTH);

ineek = caget(CalendarDAY_OF_WEEK);

Stringweekday = "";

if(week == 1) {

weekday= "星期天";

}else if (week == 2) {

weekday= "星期一";

}else if (week == 3) {

weekday= "星期二";

}else if (week == 4) {

weekday= "星期三";

}else if (week == 5) {

weekday= "星期四";

}else if (week == 6) {

weekday= "星期五";

}else if (week == 7) {

weekday= "星期六";

}

returncurrDate + " " + weekday;

}

Java怎么当前时间减过去时间

这前后时间可能是机器生成的,也可能是人工输入的,那么我们可以通过下面程式码来实现

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try

{

Date d1 = dfparse("2004-03-26 13:31:40");

Date d2 = dfparse("2004-01-02 11:30:24");

long diff = d1getTime() - d2getTime();这样得到的差值是微秒级别

long days = diff / (1000 60 60 24);

long hours = (diff-days(1000 60 60 24))/(1000 60 60);

long minutes = (diff-days(1000 60 60 24)-hours(1000 60 60))/(1000 60);

Systemoutprintln(""+days+"天"+hours+"小时"+minutes+"分");

}

catch (Exception e)

{

}

package comdadatest;

import javatextSimpleDateFormat;

import javautilCalendar;

import javautilDate;

/

  @author li_yueling

  @version 10 2011-03-25

 

 /

public class DateUtil {

/

  默认日期格式

 /

public static String DEFAULT_FORMAT = "yyyy-MM-dd";

/

  测试主方法

  @param args

 /

public static void main(String[] args) {

for(int i = 1951;i < 1960;i++){

Systemoutprintln(formatDate(getYearFirst(i)));

Systemoutprintln(formatDate(getYearLast(i)));

}

Systemoutprintln(formatDate(getCurrYearFirst()));

Systemoutprintln(formatDate(getCurrYearLast()));

}

/

  格式化日期

  @param date 日期对象

  @return String 日期字符串

 /

public static String formatDate(Date date){

SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);

String sDate = fformat(date);

return sDate;

}

/

  获取当年的第一天

  @param year

  @return

 /

public static Date getCurrYearFirst(){

Calendar currCal=CalendargetInstance();  

int currentYear = currCalget(CalendarYEAR);

return getYearFirst(currentYear);

}

/

  获取当年的最后一天

  @param year

  @return

 /

public static Date getCurrYearLast(){

Calendar currCal=CalendargetInstance();  

int currentYear = currCalget(CalendarYEAR);

return getYearLast(currentYear);

}

/

  获取某年第一天日期

  @param year 年份

  @return Date

 /

public static Date getYearFirst(int year){

Calendar calendar = CalendargetInstance();

calendarclear();

calendarset(CalendarYEAR, year);

Date currYearFirst = calendargetTime();

return currYearFirst;

}

/

  获取某年最后一天日期

  @param year 年份

  @return Date

 /

public static Date getYearLast(int year){

Calendar calendar = CalendargetInstance();

calendarclear();

calendarset(CalendarYEAR, year);

calendarroll(CalendarDAY_OF_YEAR, -1);

Date currYearLast = calendargetTime();

return currYearLast;

}

}

以上就是关于java获取当前时间几天/月/年前的时间方法全部的内容,包括:java获取当前时间几天/月/年前的时间方法、java拦截系统时间、java中怎么得到当前时间的小时等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9588996.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存