Skip to main content

Current date and Time in different formats Using Calendar

Current date and time in different formats using Calendar function

       
            Calendar c=Calendar.getInstance();
         
            //for current date

            SimpleDateFormat sdf1=new SimpleDateFormat("dd-MM-yyyy");
            String date1 = sdf1.format(c.getTime());
            //value of date1 is 27-04-2012


            SimpleDateFormat sdf_db=new SimpleDateFormat("yyyy-MM-dd");
            String date_db=sdf_db.format(c.getTime());
            //value of date_db is 2012-04-27
      
            SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMdd");
            String date2=sdf2.format(c.getTime());
            //value of date2 is 20120427


            //for current time

            SimpleDateFormat stf1=new SimpleDateFormat("hh:mm a");
            String time1 = stf1.format(c.getTime());
            //value of time1 is 04:30 pm


            SimpleDateFormat stf2=new SimpleDateFormat("HH:mm:ss");
            string time2=stf2.format(c.getTime());
            //value of time2 is 16:30:10


http://sourcecodeandroid.blogspot.in by T.s.Mathavan

Comments

Unknown said…
thanks .. pls sent some examples... if u free ... i am searching for one day for this... thanks...Good Day...

Popular posts from this blog

Sqlite database using SQLiteOpenHelper

// Sqlite database using SQLiteOpenHelpe r Creating a table named as company_table. Here company_table is table name, company.db is database name. package com.****; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class StoringCompany extends SQLiteOpenHelper {              private static final  String DATABASE_NAME = "company.db";              private static final int DATABASE_VERSION = 1;              //object for SQlite database                        SQLiteDatabase dbase;     public StoringCompany(Context context, String name, CursorFactory factory,int versio...

jio phone booking server down on 24 August 17 - Content Server error

Jio phone booking server down on 24 August 17. Showing "Content Server error".

Timer task using service

Timer task using service in android // To run a  task for a specific time in background using service in android // Example program for to display a toast message for every one minute(exactly 1 minute) //Use service in android, then only it runs in background. public class MyService extends Service {             private static Timer timer = new Timer();                       @Override             public IBinder onBind(Intent intent)            {                     // TODO Auto-generated method stub                     return null;           ...