java 的小问题

java 的小问题,第1张

Timestamp就是所谓的时间戳,这个主要用在数据库上,你可以再javasql这个包内找到这个类,一般数据库里如果用Date这个类的话,那你取出来的时候只能到某一天,也就是日,但是Timestamp的话,就是到小时一直到纳秒,很精确的。但是你把时间存进去的时候也要用这个类。比如:mysql的话,你可以用setTimtstamp();这个方法,你可以到java的文档里去看看,里面都写的比较清楚

迟来的答案

1周末版本(不含节假日判断)

注意:最下面是使用的 递归算法

/

  获得收益时间(获取当前天+1天,周末不算)

 

  @param date

             任意日期

  @return the income date

  @throws NullPointerException

              if null == date

 /

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的 *** 作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendarsetTime(date);

//+1天

calendaradd(CalendarDAY_OF_MONTH, +1);

//判断是星期几

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);

Date incomeDate = calendargetTime();

if (dayOfWeek == 1 || dayOfWeek == 7){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

测试方法:

@Test

public void testGetIncomeDate() throws ParseException{

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

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));

}

输出结果:

2014-08-01 13:48:09

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

注意:返回的是 时间+1的时间,精度是到毫秒 纳秒,如果有特殊需求,需要自己再处理下

2周末+节假日版本

/

  获得收益时间(获取当前天+1天,周末不算)

 

  @param date

             任意日期

  @return the income date

  @throws NullPointerException

              if null == date

 /

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的 *** 作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendarsetTime(date);

//+1天

calendaradd(CalendarDAY_OF_MONTH, +1);

Date incomeDate = calendargetTime();

if (isWeekend(calendar) || isHoliday(calendar)){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

/

  判断一个日历是不是周末

 

  @param calendar

             the calendar

  @return true, if checks if is weekend

 /

private boolean isWeekend(Calendar calendar){

//判断是星期几

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);

if (dayOfWeek == 1 || dayOfWeek == 7){

return true;

}

return false;

}

/

  一个日历是不是节假日

 

  @param calendar

             the calendar

  @return true, if checks if is holiday

 /

private boolean isHoliday(Calendar calendar){

String pattern = "yyyy-MM-dd";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String dateString = simpleDateFormatformat(calendargetTime());

//节假日 这个可能不同地区,不同年份 都有可能不一样,所以需要有个地方配置, 可以放数据库, 配置文件,环境变量 等等地方

//这里以配置文件 为例子

ResourceBundle resourceBundle = ResourceBundlegetBundle("holidayConfig");

String holidays = resourceBundlegetString("holiday");

String[] holidayArray = holidayssplit(",");

boolean isHoliday = orgapachecommonslangArrayUtilscontains(holidayArray, dateString);

return isHoliday;

}

配置文件:

holiday=2014-10-01,2014-10-02,2014-10-03,2014-10-04,2014-10-05,2014-10-06,2014-10-07

测试方法 :

/

  Testenclosing_type

 

  @throws ParseException

              the parse exception

 /

@Test

public void testGetIncomeDate() throws ParseException{

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

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-09-30 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-10-02 13:33:05"))));

}

结果:

2014-08-01 15:14:59

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-10-08 13:33:05

2014-10-08 13:33:05

下载JDK源代码,openJDK上有完整的JDK源代码,JDK源代码由C++、Java、C、汇编 这四种语言组成。JVM主体是C++写的,JNI部分是C,工具类是Java写的,JVM里混有汇编代码。

路径:openjdk-7-fcs-src-b147\jdk\src\share\native\java\lang\Systemc 找到这个

/ Only register the performance-critical methods /

static JNINativeMethod methods[] = {

{"currentTimeMillis", "()J",              (void )&JVM_CurrentTimeMillis},

{"nanoTime",          "()J",              (void )&JVM_NanoTime},

{"arraycopy",     "(" OBJ "I" OBJ "II)V", (void )&JVM_ArrayCopy},

};

方法的实现应该在JVM部分,路径openjdk-7-fcs-src-b147\hotspot\src\share,不同的 *** 作系统实现不一样,应该是在对应的 *** 作系统的包下,我在windows目录下找到了纳秒的实现

jlong os::javaTimeNanos() {

if (!has_performance_count) {

return javaTimeMillis() NANOS_PER_MILLISEC; // the best we can do

} else {

LARGE_INTEGER current_count;

QueryPerformanceCounter(&current_count);

double current = as_long(current_count);

double freq = performance_frequency;

jlong time = (jlong)((current/freq) NANOS_PER_SEC);

return time;

}

}

看到了这个比较有趣的东西。。。

the best we can do

我这没环境,没IDE,有环境的话,用IDE几下子就找到了。最终结论:

performance_frequency,QueryPerformanceCounter;

猜数,电脑随机产生一个1-10之间的数,你来猜是几,猜中后,输入n或n退出,其他继续猜。

public

static

int

getintinrange(string

prompt)方法

输入一个1-10之间的数,如果不在这个范围,重新输入

以上就是关于java 的小问题全部的内容,包括:java 的小问题、初学Java多线程:线程的生命周期、关于JAVA时间戳等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存