7
 string : 2014-04-25 17:03:13

using SimpleDateFormat is enough to format? or otherwise i will shift to any new API?

Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));

My expected result is (India zone):

Date : 25-04-2014
Time : 05:03 PM
Developer
  • 433
  • 3
  • 8
  • 20
  • You'll need at least two `SimpleDateFormat` objects - one to parse the initial String, and one (or maybe two) to format the result. – Dawood ibn Kareem May 20 '14 at 04:47
  • Either have two date formats... one for time and one for date. Or extend the simpledateformat and add your methods. – Hirak May 20 '14 at 04:57

4 Answers4

25

Remembering that Date objects have no inherent format, you need two DateFormat objects to produce the result you seek - one to parse and another to format:

String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy\n'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));

Output:

Date : 25-04-2014
Time : 05:03 PM

Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • actually that string comes from mysql database as datetime data type: rs.getDate("column"); can u please tell me how to handle? – Developer May 20 '14 at 05:36
  • `getDate()` returns a `java.sql.Date`, which has its time part set to `00:00:00`. Instead use `getTimestamp()`, like this: `outputFormat.format(rs.getTimestamp("column"))` – Bohemian May 20 '14 at 08:05
  • can java.sql.Time class object store time in am/pm format. if yes how? – Shuddh Jun 17 '16 at 08:17
  • 1
    @Shuddh No Java date-like objects store any formatting information; they all only represent an instant in time (typically a `long` of epoch milliseconds). Formatting is done via Format objects, although there is a default format for them (so their `toString()` works, but a) that's done at the class level (not the instance level) and b) you can't change that anyway. – Bohemian Jun 17 '16 at 20:43
3

I custom onTimeSet() function . Send the hour and minutes to it. It will return the time with format am and pm

public static String onTimeSet( int hour, int minute) {

    Calendar mCalen = Calendar.getInstance();;
       mCalen.set(Calendar.HOUR_OF_DAY, hour);
       mCalen.set(Calendar.MINUTE, minute);

       int hour12format_local = mCalen.get(Calendar.HOUR);
       int hourOfDay_local = mCalen.get(Calendar.HOUR_OF_DAY);
       int minute_local = mCalen.get(Calendar.MINUTE);
       int ampm = mCalen.get(Calendar.AM_PM);
       String minute1;
       if(minute_local<10){

        minute1="0"+minute_local;
       }
       else
          minute1=""+minute_local;

       String ampmStr = (ampm == 0) ? "AM" : "PM";
       // Set the Time String in Button

       if(hour12format_local==0)
        hour12format_local=12;



String selecteTime=hour12format_local+":"+ minute1+" "+ampmStr;

retrun selecteTime;


}
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
Pawan asati
  • 292
  • 2
  • 13
1
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

more patterns you can find here

hashplus
  • 11
  • 1
1

Try given below sample code:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); 
//Output: 2013-05-20 10:16:44

For more functionalities on Data and Time try Joda-Time API .

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Anil Satija
  • 350
  • 3
  • 13