Post Reply 
 
Thread Rating:
  • 1 Votes - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the current Date and Time
12-14-2010, 08:23 AM
Post: #1
Get the current Date and Time
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class DateUtils {
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm: ss";

public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());

}

public static void main(String arg[]) {
System.out.println("Now : " + DateUtils.now());
}
}
Find all posts by this user
Quote this message in a reply
12-14-2010, 08:24 AM
Post: #2
RE: Get the current Date and Time
(12-14-2010 08:23 AM)tutorials4u Wrote:  import java.util.Calendar;
import java.text.SimpleDateFormat;

public class DateUtils {
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm: ss";

public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());

}

public static void main(String arg[]) {
System.out.println("Now : " + DateUtils.now());
}
}


Here some formatting possibilities available through the SimpleDateFormat class.

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

public class DateUtils {


public static String now(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());

}

public static void main(String arg[]) {
System.out.println(DateUtils.now("dd MMMMM yyyy"));
System.out.println(DateUtils.now("yyyyMMdd"));
System.out.println(DateUtils.now("dd.MM.yy"));
System.out.println(DateUtils.now("MM/dd/yy"));
System.out.println(DateUtils.now("yyyy.MM.dd G 'at' hh:mm: ss z"));
System.out.println(DateUtils.now("EEE, MMM d, ''yy"));
System.out.println(DateUtils.now("h:mm a"));
System.out.println(DateUtils.now("H:mm: ss:SSS"));
System.out.println(DateUtils.now("K:mm a,z"));
System.out.println(DateUtils.now("yyyy.MMMMM.dd GGG hh:mm aaa"));
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved