编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。
function later(){ var t = prompt("请输入年月日时分秒(格式:'YYYY-MM-DD hh:mm:ss'):"); var pattern = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}$/; while(!pattern.test(t.trim())){ alert("格式错误,请重新输入!"); t = prompt("请输入年月日时分秒(格式:'YYYY-MM-DD hh:mm:ss'):"); } var later = new Date(+new Date(t)+1000), YYYY = later.getFullYear(), MM = later.getMonth() + 1, DD = later.getDate(), hh = later.getHours(), mm = later.getMinutes(), ss = later.getSeconds(); return YYYY+"-"+MM+"-"+DD+" "+hh+":"+mm+":"+ss; }
System.out.println("请输入年月日时分秒(格式:2017年09月30日23时59分59秒):"); //接收用户输入一个日期 Scanner scan=new Scanner(System.in); String before=scan.next(); /* * 将用户输入的字符串转化为日期类型并格式化 * 如果设置为"yyyy/MM/dd HH:mm:ss"这种格式,空格后面的时分秒无法读取,运行时会报错 */ SimpleDateFormat fmt=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); Calendar calendar=Calendar.getInstance(); calendar.setTime(fmt.parse(before)); //加1秒 calendar.add(Calendar.SECOND, 1); //输出2017年10月01日00时00分00秒 System.out.println("加1秒后的时间为:"+fmt.format(calendar.getTime()));
package cn.itcast.牛客; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。 * 如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。 * * @author ZLJ * */ public class TimeAdd { public static void main(String[] args) { // 创建对象 Calendar c = Calendar.getInstance(); // 2004年12月31日23时59分59秒 第一个月是0,最后一个月是11 c.set(2004, 12 - 1, 31, 23, 59, 59); Date beforetime = c.getTime(); String before = formatdemo(beforetime); System.out.println("修改前的时间是:" + before); // 添加1秒 c.add(Calendar.SECOND, 1); Date aftertime = c.getTime(); String after = formatdemo(aftertime); System.out.println("修改后的时间是:" + after); } // 格式化日期 public static String formatdemo(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");// 创建日期格式化类对象 String format = sdf.format(date); return format; } }
/* 编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。 如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。 */ SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String before = "2004/12/31 23:59:59"; Calendar calendar = Calendar.getInstance(); calendar.setTime(fmt.parse(before)); calendar.add(Calendar.SECOND, 1); System.out.println("old:" + before); System.out.println("new:" + fmt.format(calendar.getTime()));
import re import timeclass NextSecond():def next_seconf(self,str):s_list=re.findall("\d+",str)s="".join(s_list)strptime=time.strptime(s,"%Y%m%d%H%M%S")timestamp=time.mktime(strptime)new_timestamp=timestamp+1new_str=time.localtime(new_timestamp)dt=time.strftime("%Y-%m-%d %H:%M:%S", new_str)dt_list=re.findall("\d+", dt)return"%s年%s月%s日%s时%s分%s秒"% (dt_list[0], dt_list[1], dt_list[2], dt_list[3], dt_list[4], dt_list[5])if __name__=='__main__':a=NextSecond()printa.next_seconf("2017年11月09日23时53分59秒")printa.next_seconf("2004年12月31日23时59分59秒")
function Adds(date){ var newDate = new Date(new Date(date.replace('年','-').replace('月','-').replace('日',' ').replace('时',':').replace('分',':').replace('秒','')).getTime() + 1000); var y = newDate.getFullYear(); var m = newDate.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = newDate.getDate(); d = d < 10 ? ('0' + d) : d; var h = newDate.getHours(); h = h < 10 ? ('0' + h) : h; var minute = newDate.getMinutes(); minute = minute < 10 ? ('0' + minute) : minute; var second = newDate.getSeconds(); second = second < 10 ? ('0' + second) : second; return y + '年' + m + '月' + d+'日'+h+'时'+minute+'分'+second+'秒'; } Adds('2004年12月31日23时59分59秒');