黑马程序员技术交流社区
标题:
实现函数:输入年月日时分秒(字符串),输出该年月日..
[打印本页]
作者:
rightyuan521
时间:
2015-4-29 23:29
标题:
实现函数:输入年月日时分秒(字符串),输出该年月日..
public class Demo6 {
public static void main(String[] args) {
int n = 10;
String time = "2014-12-31 23:59:56";
shijian(n, time);
}
public static void shijian(int n, String time){
String[] str = time.split(" ");
String[] str1 = str[0].split("-");
String[] str2 = str[1].split(":");
int[] a = new int[6];
for (int i = 0; i < 3; i++) {
a[i] = Integer.parseInt(str1[i].trim());
a[i+3] = Integer.parseInt(str2[i].trim());
}
if (a[5]+10 < 60) {
System.out.println(time+"----n="+n+"---->"+a[0]+"-"+a[1]+"-"+a[2]+" "+a[3]+":"+a[4]+":"+(a[5]+10));
}else {
a[5] = a[5]+10 -60;
a[4] = a[4] + 1;
if (a[4]>=60) {
a[4] = a[4]-60;
a[3] = a[3] + 1;
if (a[3]>=24) {
a[3] = a[3] - 24;
a[2] = a[2] + 1;
if (a[2]>31&&(a[1] == 1 || a[1] == 3||a[1] == 5||a[1] == 7||a[1] == 8||a[1] == 10||a[1] == 12)) {
a[2] = a[2] - 31;
a[1] = a[1]+1;
if (a[1]>12) {
a[1] = a[1] -12;
a[0] = a[0] + 1;
}
}else if (a[2]>30&&(a[1] == 4 || a[1] == 6||a[1] == 9||a[1] == 11)) {
a[2] = a[2] - 30;
a[1] = a[1]+1;
if (a[1]>12) {
a[1] = a[1] -12;
a[0] = a[0] + 1;
}
}else if (a[2]>29&&(a[1] == 2)&&(a[0]%4 == 0)&&(a[0]%100 != 0)) {
a[2] = a[2] - 29;
a[1] = a[1]+1;
if (a[1]>12) {
a[1] = a[1] -12;
a[0] = a[0] + 1;
}
}else if (a[2]>28&&(a[1] == 2)&&(a[0]%4 != 0)){
a[2] = a[2] - 28;
a[1] = a[1]+1;
if (a[1]>12) {
a[1] = a[1] -12;
a[0] = a[0] + 1;
}
}
}
}
}
String[] str_result = new String[6];
for (int i = 0; i < str_result.length; i++) {
if (a[i]<10) {
str_result[i] = "0"+a[i];
}
else {
str_result[i] = ""+a[i];
}
}
System.out.println(time+"----n="+n+"---->"+str_result[0]+"-"+str_result[1]+"-"+str_result[2]+" "+str_result[3]+":"+str_result[4]+":"+(str_result[5]));
}
}
结果:
2014-12-31 23:59:56----n=10---->2015-01-01 00:00:06
2004-02-28 23:59:56----n=10---->2004-02-29 00:00:06
复制代码
实现函数:输入年月日时分秒(字符串),输出该年月日时分秒的后n秒对应的时间(字符串),比如输入2014-12- 31 23:59:56,n=10,则输出2015-01-0100:00:06。(不允许使用系统函数),输入输出的时间格式:YYYY-MM-DD hh:mm:ss 有没有更简便的代码实现??
作者:
linda2906881980
时间:
2015-4-29 23:30
这么厉害,看不懂
作者:
sandra_bae
时间:
2015-4-29 23:31
代码太长了,看得眼累啊~~
作者:
青皮核桃
时间:
2015-4-29 23:31
介么厉害,大神,求抱大腿
作者:
青皮核桃
时间:
2015-4-29 23:35
{:3_51:}{:3_50:}{:3_69:}
作者:
巴拉森
时间:
2015-4-29 23:57
厉害厉害。。。。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2