2014-08-31 3 views
-2

Есть ли способ получить только год от класса Date и разобрать его в строку? Вот код, который я использую:Как получить только год с даты Класс

Date d1 = new Date(); 
d1.getYear(); ` 

и как установить дату DatTime.Now;

как в C#?

+1

http://stackoverflow.com/questions/9474121/i-want-to-get-year-month-day-etc-from-java- date-to-compare-with-gregorian-calen – Raghunandan

+0

nop попробуйте и не работает – Dexter90

+0

опубликовать обновленный код с проверенной версией. – Raghunandan

ответ

1

SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 
System.out.println(sdf.format(new Date())); 

Выход:

2014 
+0

и как установить переменную date на текущую дату? – Dexter90

+0

@ Dexter90 Edited .. –

1
public static String getCurrentDate(String format) { 
    SimpleDateFormat sdfFrom = new SimpleDateFormat(format); 
    Calendar currentTime = Calendar.getInstance(); 
    return (sdfFrom.format(currentTime.getTime())); 
} 

public static Date getCurrentDate() { 
    Calendar currentTime = Calendar.getInstance(); 
    return currentTime.getTime(); 
} 

try to use java 8 date 
Смежные вопросы