0

I have date time format like this 2014-06-22T00:00:00

I am using this method to get month, date and year

  private String getMonthYear(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE",
            date);
        month = (String) android.text.format.DateFormat.format("MMM", date);
        day = (String) android.text.format.DateFormat.format("dd", date); // 20
        year = (String) android.text.format.DateFormat.format("yyyy", date); // 20
        Log.e("MONTH YEAR DATE", "" + dayOfTheWeek + "  " + month + "  " + day);

         String textdate = month + "\n" + day + "\n" + dayOfTheWeek;
    return textdate;
          }

but i can't able to find how to get time in am pm using this method or any other method? when i am using StringTokenizer to get the time format

     private String convertstringTodate(String date_String) {
            String dtStart = date_String;

          StringTokenizer tk = new StringTokenizer(dtStart);
                            String dateav = tk.nextToken();  
                             String timeav = tk.nextToken();
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-
     dd'T'HH:mm:ss");
         Date date = null;
          try {
             date = format.parse(dtStart);
             dtStart = getMonthYear(date);
         } catch (java.text.ParseException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
         }
         System.out.println(date);

        return dtStart;
     }

i am getting the following error

06-10 03:36:09.587: E/AndroidRuntime(1924): java.util.NoSuchElementException
06-10 03:36:09.587: E/AndroidRuntime(1924):     at    
 java.util.StringTokenizer.nextToken(StringTokenizer.java:208)
 06-10 03:36:09.587: E/AndroidRuntime(1924):    at   com.mcm.menuandnotification.EventListAdaptor.convertstringTodate(EventListAdaptor.java:133)

please help how to acheive?

Nirmal
  • 939
  • 10
  • 24

4 Answers4

4

Seeing your code, i assume what you really want is to have the String like:

2014-06-22T00:00:00

converted to

Jun
22
Sunday
00:00:00 AM

replace your method

private String convertstringTodate(String date_String) {
    SimpleDateFormat inputFormat24 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat outputFormatAmPm = new SimpleDateFormat("MMM'\n'dd'\n'EEEE'\n'KK:mm:ss a");
    try {
        Date date = inputFormat24.parse(date_String);
        return outputFormatAmPm.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}

check SimpleDateFormat documentation to see what all those MMM EEE yyy ddd etc... mean. note that you can use ticks ' to embed text, linebreaks etc. into your (output)-format e.g. "'month:'MMM'\nday:'dd"

Suau
  • 4,628
  • 22
  • 28
0
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm aa");
        String formattedDate = dateFormat.format(new Date()).toString();
        System.out.println(formattedDate);

OUTPUT 10-Jun-14 01.36 PM

This is the answer for -- not able to find how to get time in am pm using this method or any other method??

For more information SimpleDateForamt

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
  • Its giving me parse text exception – Nirmal Jun 10 '14 at 08:17
  • I tested this answer and posted here. It works completely fine, I have posted `Output` too. – Aniruddha Jun 10 '14 at 08:18
  • but you didn'tt check my string pattern its diffrent from you as you pasted – Nirmal Jun 10 '14 at 08:25
  • SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss aa");. It is in 24hour format , am/pm will be displayed because of aa, if you don't want am/pm then delete `aa`. If you want it in 12 hour format then change `HH` to `hh` – Aniruddha Jun 10 '14 at 08:30
0

It is always nice to check javadoc for StringTokenizer.

As I can see you are trying to split string for time and date. I also see that you get ISO-8601 (http://en.wikipedia.org/wiki/ISO_8601) date formatted string (from server). The issue that this string doesn't have default separators for StringTokenizer (http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html#StringTokenizer(java.lang.String)).

So do not split string but parse date first and after you could extract also format time after in am/pm localized view

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

Check out the solution -

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String date1 = "2014-06-22T00:00:00";

 try {
        d1 = format.parse(date1);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

Calendar now = Calendar.getInstance();
now.setTime(d1);

int a = now.get(Calendar.AM_PM);
if(a == Calendar.AM)
   System.out.println("AM"+now.get(Calendar.HOUR));
else
    System.out.println("PM"+now.get(Calendar.HOUR));
sjain
  • 23,126
  • 28
  • 107
  • 185