A. android實際開發中解析出來的日期格式為2013-12-12 00:00:00 我只想顯示年月日怎麼做
可以使用java中的日期格式化工具java.text.SimpleDateFormat來格式化,以下為示例代碼:
創建java.text.SimpleDateFormat實例
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
參數說明,"yyyy-MM-dd"表示日期的格式,也可以是這樣的 yyyy-MM-dd HH:mm:ss注意大小寫
2.將Date對象轉成字元串
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);
3.將字元串轉成Date
String str = "2013-12-12";
Date date = sdf.parse(str);
System.out.prinltn(date);
注意,SimpleDateFormat解析工具,指定的format以後,只能解析指定格式的日期,例如指定了yyyy-MM-dd則不可以再去解析 yyyy-MM-dd HH:mm:ss格式。
B. android 怎麼把日期字元串解析出 月份和日期
//需要解析的日期字元串
StringdateStr="2015-09-2712:15:31";
//解析格式,yyyy表示年,MM(大寫M)表示月,dd表示天,HH表示小時24小時制,小寫的話是12小時制
//mm,小寫,表示分鍾,ss表示秒
SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
try{
//用parse方法,可能會異常,所以要try-catch
Datedate=format.parse(dateStr);
//獲取日期實例
Calendarcalendar=Calendar.getInstance();
//將日歷設置為指定的時間
calendar.setTime(date);
//獲取年
intyear=calendar.get(Calendar.YEAR);
//這里要注意,月份是從0開始。
intmonth=calendar.get(Calendar.MONTH);
//獲取天
intday=calendar.get(Calendar.DAY_OF_MONTH);
}catch(ParseExceptione){
e.printStackTrace();
}
C. android 的DatePicker怎麼取出 年月日的值
private TextView t;//定義一個textview控制項
private DatePicker dp;//日期控制項
dp=(DatePicker)findViewById(R.id.datePicker1);取得id
t=(TextView)findViewById(R.id.text)取得id
dp.init(ca.get(Calendar.YEAR),ca.get(Calendar.MONTH),
ca.get(Calendar.DAY_OF_MONTH),
new DatePicker.OnDateChangedListener() {
//監聽選擇的出生日期
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
ca.set(year, monthOfYear,dayOfMonth);//改變日期
t.setText(year+"年"+monthOfYear+"月"+dayOfMonth+"日");
//取得值
}
});
D. android DatePickerDialog怎麼設置日期選擇范圍。
時間設置對話框DatePickerDialog的使用
一、可以同過重載DatePickerDialog的setTitle來設置個性的標題
比如:
@Override
public void setTitle(CharSequence title) {
// TODO Auto-generated method stub
java.text.DateFormat dataFormat = (DateFormat
.getDateFormat(getContext()));
mCalendar = Calendar.getInstance();
//可得到「12/31/1969(Wed)」形式的日期格式
String strTitle=dataFormat.format(mCalendar.getTime())
+ DateFormat.format(" (E)", mCalendar.getTime()).toString();
super.setTitle(dataFormat.format(strTitle);
}
二、通過onDateChanged回調函數監聽時間的改變
當時間改變時(編輯) 以下函數回被調用
public void onDateChanged(DatePicker view, int year, int month, int day) {
Log.i("hubin","onDateChanged");
}
三、通過updateDate()設置年月日
public void updateDate (int year, int monthOfYear, int dayOfMonth)設置DatePickerDialog的年,月,日
四、通過onDateChangedListener來監聽時間設置對話框的確定按鈕被按下
onDateChangedListener的onDateSet是用於都在時間設置對話框的確定按鈕被按下的監聽
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class TestClock extends Activity implements OnDateSetListener, OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.date);
btn.setOnClickListener(this);
}
public void onClick(View v) {//普通按鈕事件
Calendar d = Calendar.getInstance(Locale.CHINA);
//創建一個日歷引用d,通過靜態方法getInstance() 從指定時區 Locale.CHINA 獲得一個日期實例
Date myDate=new Date();
//創建一個Date實例
d.setTime(myDate);
//設置日歷的時間,把一個新建Date實例myDate傳入
int year=d.get(Calendar.YEAR);
int month=d.get(Calendar.MONTH);
int day=d.get(Calendar.DAY_OF_MONTH);
//獲得日歷中的 year month day
DatePickerDialog dlg=new DatePickerDialog(this,this,year,month,day);
//新建一個DatePickerDialog 構造方法中
// (設備上下文,OnDateSetListener時間設置監聽器,默認年,默認月,默認日)
dlg.show();
//讓DatePickerDialog顯示出來
}
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth){
//DatePickerDialog 中按鈕Set按下時自動調用
TextView txt = (TextView) findViewById(R.id.text);
//通過id獲得TextView對象
txt.setText(Integer.toString(year) + "-" +
Integer.toString(monthOfYear) + "-" +
Integer.toString(dayOfMonth));
//設置text
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/text"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
android:text="設置時間"
>
</Button>
</LinearLayout>
E. android中在編輯框怎樣獲取日期
這取決於對日期的格式定義
假如日期格式為2015-12-09即為2015年12月9日
可以使用SimpleDateFormat把字元串格式化轉為日期
示例如下
{
privateEditTextetDate;
privateButtonbtn;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etDate=(EditText)findViewById(R.id.text);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Stringtxt=etDate.getText().toString();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
Datedate=sdf.parse(txt);
}
});
}
}
SimpleDateFormat的語法格式
G 年代標志符
y 年
M 月
d 日
h 時 在上午或下午 (1~12)
H 時 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個星期幾
w 一年中第幾個星期
W 一月中第幾個星期
a 上午 / 下午 標記符
k 時 在一天中 (1~24)
K 時 在上午或下午 (0~11)
z 時區
F. android 怎麼比較日期選擇器的選擇的時間和系統當前的時間
package com.wwj.datetimepicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 日期時間選擇控制項 使用方法: private EditText inputDate;//需要設置的日期時間文本編輯框 private String
* initDateTime="2012年9月3日 14:44",//初始日期時間值 在點擊事件中使用:
* inputDate.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { DateTimePickDialogUtil
* dateTimePicKDialog=new
* DateTimePickDialogUtil(SinvestigateActivity.this,initDateTime);
* dateTimePicKDialog.dateTimePicKDialog(inputDate);
*
* } });
*
* @author
*/
public class DateTimePickDialogUtil implements OnDateChangedListener,
OnTimeChangedListener {
private DatePicker datePicker;
private TimePicker timePicker;
private AlertDialog ad;
private String dateTime;
private String initDateTime;
private Activity activity;
/**
* 日期時間彈出選擇框構造函數
*
* @param activity
* :調用的父activity
* @param initDateTime
* 初始日期時間值,作為彈出窗口的標題和日期時間初始值
*/
public DateTimePickDialogUtil(Activity activity, String initDateTime) {
this.activity = activity;
this.initDateTime = initDateTime;
}
public void init(DatePicker datePicker, TimePicker timePicker) {
Calendar calendar = Calendar.getInstance();
if (!(null == initDateTime || "".equals(initDateTime))) {
calendar = this.getCalendarByInintData(initDateTime);
} else {
initDateTime = calendar.get(Calendar.YEAR) + "年"
+ calendar.get(Calendar.MONTH) + "月"
+ calendar.get(Calendar.DAY_OF_MONTH) + "日 "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE);
}
datePicker.init(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), this);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
/**
* 彈出日期時間選擇框方法
*
* @param inputDate
* :為需要設置的日期時間文本編輯框
* @return
*/
public AlertDialog dateTimePicKDialog(final EditText inputDate) {
LinearLayout dateTimeLayout = (LinearLayout) activity
.getLayoutInflater().inflate(R.layout.common_datetime, null);
datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
init(datePicker, timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
ad = new AlertDialog.Builder(activity)
.setTitle(initDateTime)
.setView(dateTimeLayout)
.setPositiveButton("設置", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText(dateTime);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText("");
}
}).show();
onDateChanged(null, 0, 0, 0);
return ad;
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
onDateChanged(null, 0, 0, 0);
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// 獲得日歷實例
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
dateTime = sdf.format(calendar.getTime());
ad.setTitle(dateTime);
}
/**
* 實現將初始日期時間2012年07月02日 16:45 拆分成年 月 日 時 分 秒,並賦值給calendar
*
* @param initDateTime
* 初始日期時間值 字元串型
* @return Calendar
*/
private Calendar getCalendarByInintData(String initDateTime) {
Calendar calendar = Calendar.getInstance();
// 將初始日期時間2012年07月02日 16:45 拆分成年 月 日 時 分 秒
String date = spliteString(initDateTime, "日", "index", "front"); // 日期
String time = spliteString(initDateTime, "日", "index", "back"); // 時間
String yearStr = spliteString(date, "年", "index", "front"); // 年份
String monthAndDay = spliteString(date, "年", "index", "back"); // 月日
String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日
String hourStr = spliteString(time, ":", "index", "front"); // 時
String minuteStr = spliteString(time, ":", "index", "back"); // 分
int currentYear = Integer.valueOf(yearStr.trim()).intValue();
int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
int currentDay = Integer.valueOf(dayStr.trim()).intValue();
int currentHour = Integer.valueOf(hourStr.trim()).intValue();
int currentMinute = Integer.valueOf(minuteStr.trim()).intValue();
calendar.set(currentYear, currentMonth, currentDay, currentHour,
currentMinute);
return calendar;
}
/**
* 截取子串
*
* @param srcStr
* 源串
* @param pattern
* 匹配模式
* @param indexOrLast
* @param frontOrBack
* @return
*/
public static String spliteString(String srcStr, String pattern,
String indexOrLast, String frontOrBack) {
String result = "";
int loc = -1;
if (indexOrLast.equalsIgnoreCase("index")) {
loc = srcStr.indexOf(pattern); // 取得字元串第一次出現的位置
} else {
loc = srcStr.lastIndexOf(pattern); // 最後一個匹配串的位置
}
if (frontOrBack.equalsIgnoreCase("front")) {
if (loc != -1)
result = srcStr.substring(0, loc); // 截取子串
} else {
if (loc != -1)
result = srcStr.substring(loc + 1, srcStr.length()); // 截取子串
}
return result;
}
}
/DateTimePicker/src/com/wwj/datetimepicker/DateTimePickerActivity.java
[java] view plain
package com.wwj.datetimepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
/**
* 時間拾取器界面
*
* @author wwj_748
*
*/
public class DateTimePickerActivity extends Activity {
/** Called when the activity is first created. */
private EditText startDateTime;
private EditText endDateTime;
private String initStartDateTime = "2013年9月3日 14:44"; // 初始化開始時間
private String initEndDateTime = "2014年8月23日 17:44"; // 初始化結束時間
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 兩個輸入框
startDateTime = (EditText) findViewById(R.id.inputDate);
endDateTime = (EditText) findViewById(R.id.inputDate2);
startDateTime.setText(initStartDateTime);
endDateTime.setText(initEndDateTime);
startDateTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
DateTimePickerActivity.this, initEndDateTime);
dateTimePicKDialog.dateTimePicKDialog(startDateTime);
}
});
endDateTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
DateTimePickerActivity.this, initEndDateTime);
dateTimePicKDialog.dateTimePicKDialog(endDateTime);
}
});
}
}
自定義界面:
/DateTimePicker/res/layout/common_datetime.xml
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false" />
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
源碼下載:http://download.csdn.net/detail/wwj_748/7806993
G. 在android中如何獲取當前日期
Android中獲取系統時間和日期,星期代碼如下:
import java.text.SimpleDateFormat;
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//獲取當前時間
String str = formatter.format(curDate);
可以獲取當前的年月時分,也可以分開寫:
復制代碼 代碼如下:
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());
如果想獲取當前的年月,則可以這樣寫(只獲取時間或秒種一樣):
Java代碼
復制代碼 代碼如下:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
String date=sdf.format(new java.util.Date());
當然還有就是可以指定時區的時間(待):
復制代碼 代碼如下:
df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);
System.out.println(df.format(new Date()));
如何獲取Android系統時間是24小時制還是12小時制
復制代碼 代碼如下:
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24"))
{
Log.i("activity","24");
}
復制代碼 代碼如下:
Calendar c = Calendar.getInstance();
取得系統日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
利用Calendar獲取
復制代碼 代碼如下:
Calendar c = Calendar.getInstance();
取得系統日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
Calendar c = Calendar.getInstance();
取得系統日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
利用Time獲取
復制代碼 代碼如下:
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone資料。
t.setToNow(); // 取得系統時間。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
H. android 中 \/Date(-62135596800000)\/ 這種日期格式該怎麼解析
這種日期格式該這樣解析
String str="62135596800000";
long dateLong=Long.parseLong(str);
Date date = new Date(dateLong);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr=formatter.format(date);
tv.setText(dateStr);
你也可看下下面,對學習有幫助
<code>Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));</code>
注意此處應該應該用 android.text.format.DateFormat 而不是 java.text.DateFormat.
2. 如何自定義日期格式:
<code>event.putExtra("starttime", "12/18/2012");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));</code>
採納吧!
I. android開發,long型時間怎麼取出對應的年月日
long類型的時間說明獲取得到的是時間戳,具體轉換可參考以下代碼
//mill為你龍類型的時間戳
Datedate=newDate(mill);
Stringstrs="";
try{
//yyyy表示年MM表示月dd表示日
//yyyy-MM-dd是日期的格式,比如2015-12-12如果你要得到2015年12月12日就換成yyyy年MM月dd日
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
//進行格式化
strs=sdf.format(date);
System.out.println(strs);
}catch(Exceptione){
e.printStackTrace();
}
J. 如何將android時間戳轉換成時間
時間戳就是如1377216000000 這種格式我們在mysql資料庫中會經常用到把時間轉換成時間戳或把時間戳轉換成日期格式了,下面我來介紹安卓中時間戳操作轉換方法。
一、原理
時間戳的原理是把時間格式轉為十進制格式,這樣就方便時間的計算。好~ 直接進入主題。(下面封裝了一個類,有需要的同學可以參考或是直接Copy 就可以用了。)
如: 2013年08月23日 轉化後是 1377216000000
二、步驟
1、創建 DateUtilsl類。
代碼如下 復制代碼
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
/*
* @author Msquirrel
*/
public class DateUtils {
privateSimpleDateFormat sf = null;
/*獲取系統時間 格式為:"yyyy/MM/dd "*/
public static String getCurrentDate() {
Date d = newDate();
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*時間戳轉換成字元竄*/
public static String getDateToString(long time) {
Date d = newDate(time);
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*將字元串轉為時間戳*/
public static long getStringToDate(String time) {
sdf = newSimpleDateFormat("yyyy年MM月dd日");
Date date = newDate();
try{
date = sdf.parse(time);
} catch(ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returndate.getTime();
}
2、在對應使用的地方調用就可以了。
代碼如下 復制代碼
DateUtils.getCurrentDate(); //獲取系統當前時間
DateUtils.getDateToString(時間戳); //時間戳轉為時間格式
DateUtils.getStringToDate("時間格式");//時間格式轉為時間戳