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

Interacting with delegated QML Component in Repeater from C++

c++ qt qml repeater qqmlcomponent

I can't access to delegated QML Component in Repeater from C++. Please find codes below. Thanks.

How to send data to Redis from a Resque worker?

ruby-on-rails redis resque

I'm trying to create a resque worker that does some webscraping as a background job. The worker receives the URL for a website's homepage, does some webscraping, and then stores the results in Red...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.