Date Formatting in C#

Date Formatting in Java

Posted on Updated on

Hi All ..

hope enjoying time ..

today i will explain how to use date time (Date Manipulation) in java programming language. I was working on a task where i need to play with time/date. In my other post i explained  Date Formatting in C#. Today you will learn same concept in java.

Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class. First get current date time with Calendar()

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(“Current Date Time : ” + dateFormat.format(cal.getTime()));

now you can use following calender date time manipulation function as per your need

//Add one day to current date time
cal.add(Calendar.DATE, 1);

//Add one month to current date time
cal.add(Calendar.MONTH, 1);

//Add one year to current date time
cal.add(Calendar.YEAR, 1);

//Add one hour to current date time
cal.add(Calendar.HOUR, 1);

//Add one minute to current date time
cal.add(Calendar.MINUTE, 1);

//Add one second to current date time
cal.add(Calendar.SECOND, 1);

//Subtract one day from current date
cal.add(Calendar.DATE, -1);

//Subtract one month from current date
cal.add(Calendar.MONTH, -1);

//Subtract one year from current date
cal.add(Calendar.YEAR, -1);

//Subtract one hour from current date
cal.add(Calendar.HOUR, -1);

//Subtract one minute from current date
cal.add(Calendar.MINUTE, -1);

//Subtract one second from current date
cal.add(Calendar.SECOND, -1);

Here is the full source code to show how to modify date time in Java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTimeManipulation {
public static void main(String[] args) {

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(“Current Date Time : ” + dateFormat.format(cal.getTime()));

cal.add(Calendar.DATE, 1);
System.out.println(“Add one day to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
System.out.println(“Add one month to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
System.out.println(“Add one year to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
System.out.println(“Add one hour to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);
System.out.println(“Add one minute to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 1);
System.out.println(“Add one second to current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(“Subtract one day from current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
System.out.println(“Subtract one month from current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
System.out.println(“Subtract one year from current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
System.out.println(“Subtract one hour from current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -1);
System.out.println(“Subtract one minute from current date : ” + dateFormat.format(cal.getTime()));

cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -1);
System.out.println(“Subtract one second from current date : ” + dateFormat.format(cal.getTime()));

}
}

Output

Current Date Time : 2010/08/13 10:24:53
Add one day to current date : 2010/08/14 10:24:53
Add one month to current date : 2010/09/13 10:24:53
Add one year to current date : 2011/08/13 10:24:53
Add one hour to current date : 2010/08/13 11:24:53
Add one minute to current date : 2010/08/13 10:25:53
Add one second to current date : 2010/08/13 10:24:54
Subtract one day from current date : 2010/08/12 10:24:53
Subtract one month from current date : 2010/07/13 10:24:53
Subtract one year from current date : 2009/08/13 10:24:53
Subtract one hour from current date : 2010/08/13 09:24:53
Subtract one minute from current date : 2010/08/13 10:23:53
Subtract one second from current date : 2010/08/13 10:24:52
free counters

Date Formatting in C#

Posted on

Hello all.

hope doing well …

here is another tip related to C# programming …. i found it useful so sharing with you people …   this is how you can format date in different format in asp.net / C#

the usage is as follows

<%= String.Format(“{specifier}”, DateTime.Now) %>

where the possible specifiers are given below.

Specifier Description Output
d Short Date 15/05/2010
D Long Date 15 June 2010
t Short Time 21:08
T Long Time 21:08:59
f Full date and time 15 June 2010 21:08
F Full date and time (long) 15 June 2010 21:08:59
g Default date and time 15/06/2010 21:08
G Default date and time (long) 15/06/2010 21:08:59
M Day / Month 15 June
r RFC1123 date Tue, 15 June 2010 21:08:59 GMT
s Sortable date/time 2010-06-15 21:08:59
u Universal time, local timezone 2010-06-15 21:08:59Z
Y Month / Year June 2010
dd Day 15
ddd Short Day Name Sun
dddd Full Day Name Sunday
hh 2 digit hour 09
HH 2 digit hour (24 hour) 21
mm 2 digit minute 08
MM Month 04
MMM Short Month name Jun
MMMM Month name June
ss seconds 59
tt AM/PM PM
yy 2 digit year 07
yyyy 4 digit year 2010
: seperator, e.g. {0:hh:mm:ss} 09:08:59
/ seperator, e.g. {0:dd/MM/yyyy} 15/06/2010

happy development 🙂

free counters