반응형
반응형

String으로 된 날짜에 일자를 더해서 세팅

package com.blue.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CalendarEx2 { 

       public static void main(String[] arg) { 

             // 2019.12.31 + 2일
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
             Date date = null;
             try {
                    date = dateFormat.parse("20191231");
             } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }

             Calendar cal = Calendar.getInstance();
             cal.setTime(date);
             cal.add(Calendar.DATE, 2);
             System.out.println(date);
             System.out.println(cal.getTime());
            

             String strDate = dateFormat.format(cal.getTime());
             System.out.println(strDate);
 

       }


}
반응형
반응형

jsp 현재 날짜, 일주일전 날짜, 한달 전 날짜 구하기.

currentCalendar.add 부분에 -값이 아닌 +값을 대입하면 현재 이후의 날짜를 구할 수 있지요.

 

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Calendar" %>

<%
    DecimalFormat df = new DecimalFormat("00");
    Calendar currentCalendar = Calendar.getInstance();

  //현재 날짜 구하기
    String strYear   = Integer.toString(currentCalendar.get(Calendar.YEAR));
    String strMonth  = df.format(currentCalendar.get(Calendar.MONTH) + 1);
    String strDay   = df.format(currentCalendar.get(Calendar.DATE));
    String strDate = strYear + strMonth + strDay;

  //일주일 전 날짜 구하기
    currentCalendar.add(currentCalendar.DATE, -7);
    String strYear7   = Integer.toString(currentCalendar.get(Calendar.YEAR));
    String strMonth7  = df.format(currentCalendar.get(Calendar.MONTH) + 1);
    String strDay7   = df.format(currentCalendar.get(Calendar.DATE));
    String strDate7 = strYear7 + strMonth7 + strDay7;

  //한달 전 날짜 구하기
    currentCalendar.add(currentCalendar.DATE, -24);
    String strYear31   = Integer.toString(currentCalendar.get(Calendar.YEAR));
    String strMonth31  = df.format(currentCalendar.get(Calendar.MONTH) + 1);
    String strDay31   = df.format(currentCalendar.get(Calendar.DATE));
    String strDate31 = strYear31 + strMonth31 + strDay31;
%>

<!-- 현재날짜 -->
<c:set var="nowdate" value='<%=strDate%>' />
<!-- 일주일전 -->
<c:set var="nowdate7" value='<%=strDate7%>' />
<!-- 한달전 -->
<c:set var="nowdate31" value='<%=strDate31%>' /> 
반응형
반응형

어제 날짜 구하기 


#! /usr/bin/python2.7

# -*- coding: utf-8 -*-


#import datetime

#now = datetime.datetime.now()

#dt = now.strftime('%Y-%m-%d_%H:%M:%S')


import time

from datetime import date

today = date.today()

yesterday = date.fromtimestamp(time.time() - 60*60*24)

dty = yesterday.strftime('%Y-%m-%d'))

dty = yesterday.strftime('%Y%m%d')

dt = today.strftime('%Y-%m-%d')

dt = today.strftime('%Y%m%d')


print(" dty : "+ dty )

print(" dt : "+ dt )


.

반응형
반응형

ASP 날짜, 시간 - date(), Now(), time()

 

    now_a = Now()
    now_b = Left(Now(), 10)
    now_c = Mid(Date(),1,4) & Mid(Date(),6,2) & Mid(Date(),9,2) & left(FormatDateTime(Now(), 4),2)

                right(FormatDateTime(Now(), 4),2) & right(FormatDateTime(Now(), 3),2)


    response.write " now_a = "& now_a
    response.write " now_b = "& now_b
    response.write "<br> now_c = "& now_c

 

 

Now() ==> 2009-07-09 오후 4:48:49
Date() ==> 2009-07-09
Time() ==> 오후 4:48:49

FormatDateTime(Now(), 0) ==> 2009-07-09 오후 4:48:49
FormatDateTime(Now(), 1) ==> 2009년 7월 9일 목요일
FormatDateTime(Now(), 2) ==> 2009-07-09
FormatDateTime(Now(), 3) ==> 오후 4:48:49
FormatDateTime(Now(), 4) ==> 16:48

 

* YYYY-MM-DD HH24:MI:SS (오라클 기준)
FormatDateTime(Now(), 2) ==> 2009-07-09
FormatDateTime(Now(), 4) ==> 16:48
Right(Now(), 3) ==> :49
====> 2009-07-09 16:48:49

 

Left(Date(), 4) ==> 2009 (년)
Mid(Date(), 6, 2) ==> 07 (월)
Int(Mid(Date(), 6, 2)) ==> 7 (월)

반응형

+ Recent posts