0

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?

Sid
  • 21
  • 1
  • 8

4 Answers4

1

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);
anirudh
  • 4,116
  • 2
  • 20
  • 35
0

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());
furkick
  • 423
  • 6
  • 18
0

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);   
   }
}
Baklap4
  • 3,914
  • 2
  • 29
  • 56
Giridharan
  • 4,402
  • 5
  • 27
  • 30
0

Try this Hope this will help you

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

Thanks :)

M A.
  • 949
  • 2
  • 12
  • 29