简体   繁体   English

在Android中设置当前日期

[英]Setting Current Date in Android

I am using the below code to get a formatted date as 31-12-2014. 我正在使用以下代码获取格式化的日期为2014年12月31日。

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? 当我记录日志时, mDummyDate的年份设置为1970。可能是什么原因?

Try this code.. I get 31-12-2014 as output. 尝试这段代码。.我得到31-12-2014作为输出。

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"); SimpleDateFormat sdf = new SimpleDateFormat(“ yyyyMMdd_HHmmss”); String currentDateandTime = sdf.format(new Date()); 字符串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 :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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