/**

* 判断是否为闰年

* @param year

* @return

*/

public boolean isLeap ( int year )

{

if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )

return true;

else

return false;

}

/**

* 判断某年某月总天数

* @param year

* @param month

* @return

*/

public int getDays ( int year , int month )     {

int tianshu = 0;

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

tianshu = 31;

break;

case 4:

case 6:

case 9:

case 11:

tianshu = 30;

break;

case 2:

if ( isLeap(year) == true )

{

tianshu = 29;

}

else

{

tianshu = 28;

}

break;

}

return tianshu;

}

/**

* 在某Date()日期上增加几天或几个月

* @return 新的日期

*/

public static String changeDate(String str, int n) {

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

try {

Calendar cd = Calendar.getInstance();

cd.setTime(sdf.parse(str));

cd.add(Calendar.DATE, n); // 在审批基础上增加一天

// cd.add(Calendar.MONTH, n);//增加一个月

return sdf.format(cd.getTime());

} catch (Exception e) {

Global.getInstance().LogError(e);

return str;

}

}

//两个Date()相加减

public static void main(String[] args) {

try {

SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");

Date begin=dfs.parse("1992-08-10");

Date end =  new Date();

long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒

long day1=between/(24*3600);       //几天

long hour1=between%(24*3600)/3600; //零几小时

long minute1=between%3600/60;      //零几分钟

long second1=between%60/60;        //零几秒钟

} catch (ParseException e) {

e.printStackTrace();

}

}

//输入某人生日计算实际年龄

public static int getAge(String date) {

try {

SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");

Date begin = dfs.parse(date);

int month = begin.getMonth() + 1;

int day = begin.getDate();

Date end = new Date();

long between = (end.getTime() - begin.getTime()) / 1000;

long day1 = between / (24 * 3600);

int age = (int) (day1 / 365);

long nmonth = end.getMonth() + 1;

long nday = end.getDate();

if (nmonth < month || (nmonth == month && day < nday)) {

age--;

}

return age;

} catch (Exception e) {

e.printStackTrace();

return 0;

}

}

转成这样时间格式:二〇一四年九月二十一日

this.getCurrentDate = function() {
var today = new Date();
var chinese = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
var y = today.getFullYear().toString();
var m = (today.getMonth() + 1).toString();
var d = today.getDate().toString();
var result = "";
for (var i = 0; i < y.length; i++) {
result += chinese[y.charAt(i)];
}
result += "年";
if (m.length == 2) {
if (m.charAt(0) == "1") {
result += ("十" + chinese[m.charAt(1)] + "月");
}
} else {
result += (chinese[m.charAt(0)] + "月");
}
if (d.length == 2) {
result += (chinese[d.charAt(0)] + "十" + chinese[d.charAt(1)] + "日");
} else {
result += (chinese[d.charAt(0)] + "日");
}
return result;
};

Date()日期转换和简单计算的更多相关文章

  1. Struts2中Date日期转换的问题

      今天跑程序的时候莫名其妙的出现了下面的一个异常: java.lang.NoSuchMethodException:com.ca.agent.model.mybatis.ApprovalInforC ...

  2. Shell-匹配行及date日期转换

    #将指定字符串转化为从1970年1月1日到现在的秒数. date -d '20170506' "+%s" #将1970年1月1日到现在累计的秒数转化为日期 date -d @149 ...

  3. 日期转换工具类 CommUtil.java

    package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...

  4. js中转换Date日期格式

    在javascript中直接输出Date得到的结果是这样的: function date(){ var date = new Date(); alert(date); } 结果是:Mon Jun 15 ...

  5. 日期转换时Safari中返回Invalid Date

    问题: 进行日期转换时,Safari中会返回Invalid Date, 而IE 9, Firefox, Chrome and Opera显示正常,代码如下所示: var d = new Date(&q ...

  6. SpringMVC解决@ResponseBody返回Json的Date日期类型的转换问题

    在做项目的时候,发现后台把Date类型的属性以json字符串的形式返回,前台拿不到转换后的日期格式,始终响应回去的都是long类型时间戳. 查阅资料之后找到解决方法: 方法一(在springmvc的x ...

  7. js转换Date日期格式

    有时候做项目会用到js的date日期格式,因为Date()返回的格式不是我们需要的, Date()返回格式: Thu Mar 19 2015 12:00:00 GMT+0800 (中国标准时间) 而我 ...

  8. 随记MySQL的时间差函数(TIMESTAMPDIFF、DATEDIFF)、日期转换计算函数(date_add、day、date_format、str_to_date)

    时间差函数(TIMESTAMPDIFF.DATEDIFF) 需要用MySQL计算时间差,使用TIMESTAMPDIFF.DATEDIFF,记录一下实验结果 select datediff(now(), ...

  9. java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串

    package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...

随机推荐

  1. mongo之find结果对象map实例处理

    find 找到结果对象列表 res = await SS_StudentsLeaveTask.find(filter=_filter) self.resData = map(lambda x: str ...

  2. 一起感受HTML5和CSS3

    Web设计师可以使用HTML4和CSS2.1完成一些很酷的东西.我们可以在不使用陈旧的基于table布局的基础上完成文档逻辑结构并创建内容丰富的网站.我们可以在不使用内联<font>和&l ...

  3. 2018-8-10-wpf-DoEvents-

    title author date CreateTime categories wpf DoEvents lindexi 2018-08-10 19:16:51 +0800 2018-2-13 17: ...

  4. 引用opencv异常

    1.异常AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d' 原因:**3.X以后OpenCv只包含部分内容,需要神经网络或者 ...

  5. 34. Thread类的常用方法

    1.构造方法 Thread() 分配新的 Thread 对象. Thread(String name)  分配新的 Thread 对象并指定线程名字 2.方法 1)setName(String nam ...

  6. Web API 接口参考

    Web API 接口参考:https://developer.mozilla.org/zh-CN/docs/Web/API

  7. 代码格式化工具---prettier配置

    我自己的常用 prettier 配置如下: // .prettierrc 文件 // 这里修改的都是与默认值不同的,没有修改到的就是启用默认值 // .prettierrc 文件是使用 json 格式 ...

  8. dfs套异或的包含性——cf986C好

    很好的题,想了半天,官方题解的解法更好 这种异或问题的包含性在北邮的校赛里就出现过,需要认真学习一下 /* y和所有合法的x合并,如果没有剪枝,那么复杂度爆炸总共要判(2^n*2^n) 可以考虑如下优 ...

  9. NX二次开发-UFUN计时函数UF_begin_timer

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //计时开始 UF_timer_t Timer ...

  10. P1910 L国的战斗之间谍

    P1910 L国的战斗之间谍 题目背景 L国即将与I国发动战争!! 题目描述 俗话说的好:“知己知彼,百战不殆”.L国的指挥官想派出间谍前往I国,于是,选人工作就落到了你身上. 你现在有N个人选,每个 ...