Format current date to show day of the week

I want to show the current date in my application like this: Thu, May 2, 2013

I already have the following code to get the current date

Calendar c = Calendar.getInstance();

Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
             c.get(Calendar.YEAR));

How can I format this Time object to the string I need?

Answers


This does what you want

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);

Where strDate can be displayed in your textView or whatever


Maybe you can use it.

This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.

import java.text.*;
import java.util.*;
public class Main {
   public static void main(String[] args) {
      Date dt = new Date(1000000000000L);

      DateFormat[] dtformat = new DateFormat[6];
      dtformat[0] = DateFormat.getInstance();
      dtformat[1] = DateFormat.getDateInstance();
      dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
      dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
      dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
      dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);

      for(DateFormat dateform : dtformat)
         System.out.println(dateform.format(dt));
  }
}

output:

9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01

Source


Need Your Help

Connect to database using jsf

java mysql database maven-2

How do I connect to the database(MYSQL) in connection bean using JSF to retrieve its contents. Also please let me know how do I configure the web.xml file?

php create dynamic html table

php html json table row

I need to create a dynamic html table using PHP after parsing a JSON file. I need this column structure for my table; Name | Status | Age | Count | Progress | Bad

How to prevent System.Windows.Forms.NumericUpDown from allowing to type non-digits when DecimalPlaces = 0?

winforms .net-4.0 validation numericupdown decimal-point

I wish to make this WinForm control (NumericUpDown) enforce whole numbers conditionally depending on the type (whole vs floating) of the thing that I am working with.