简体   繁体   中英

Setting Current Date in Android

I am using the below code to get a formatted date as 31-12-2014.

private Calendar mDummyDate;
mDummyDate = Calendar.getInstance();
mDummyDate.set(mDummyDate.get(Calendar.YEAR), 11, 31);

When i take logs, the year in mDummyDate is set as 1970. What could be the reason?

Try this code.. I get 31-12-2014 as output.

Calendar mDummyDate;
mDummyDate = Calendar.getInstance();
mDummyDate.set(mDummyDate.get(Calendar.YEAR), 11, 31);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = dateFormat.format(mDummyDate.getTime());
System.out.println(formattedDate);

Hi try this code and it should call your systems date to pull the information

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = dateFormat.format(c.getTime());

If you want to get the date and time in a specific pattern you can use the following:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String currentDateandTime = sdf.format(new Date());

try this..

package com.xcart;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;    

public class GetCurrentDateTime {
   public static void main(String[] args) {    
       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
       //get current date time with Date()
       Date date = new Date();
       //System.out.println(dateFormat.format(date)); don't print it, but save it!
       String yourDate = dateFormat.format(date);   
   }
}

Try this Hope this will help you

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = df.format(Calendar.getInstance().getTime()); 

Thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM