当前位置: 编程技术>移动开发
本页文章导读:
▪Java日期格式化及其施用例子收集【转】 Java日期格式化及其使用例子收集【转】
1 SimpleDateFormat担当重任,怎样格式化都行
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo
{
public static void main(String[] args)
{
Date now=new Date();.........
▪ 菜单的容易入门 菜单的简单入门
public class WidgetActivity extends Activity {
/** Called when the activity is first created. */
private final String TAG = "WidgetActivity...";
private Menu myMenu;
@Override
public void onCreate(Bundle savedInst.........
▪ 创设一个简单的widget(转载自mars) 创建一个简单的widget(转载自mars)
1 、创建一个AppWidgetProvider
public class ExampleAppWidgetProvider extends AppWidgetProvider {
//定义一个常量字符串,该常量用于命名Action
private static final String UPDAT.........
[1]Java日期格式化及其施用例子收集【转】
来源: 互联网 发布时间: 2014-02-18
Java日期格式化及其使用例子收集【转】
5 按本地时区输出当前日期
7 在数据库里的日期只以年-月-日的方式输出
8 经典例子(http://blog.csdn.net/donkeyzheng/archive/2005/12/30/566470.aspx)
1 SimpleDateFormat担当重任,怎样格式化都行
import java.util.Date; import java.text.SimpleDateFormat; public class Demo { public static void main(String[] args) { Date now=new Date(); SimpleDateFormat f=newSimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分"); System.out.println(f.format(now)); f=new SimpleDateFormat("a hh点mm分ss秒"); System.out.println(f.format(now)); } }2 从字符串到日期类型的转换:
import java.util.Date; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import java.text.*; publicclass Demo { public static void main(String[] args) { String strDate="2005年04月22日"; //注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日"); //必须捕获异常
try { Date date=simpleDateFormat.parse(strDate); System.out.println(date); } catch(ParseException px) { px.printStackTrace(); } } }3 将毫秒数换转成日期类型
import java.util.Date; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import java.text.*; public class Demo { public static void main(String[] args) { long now=System.currentTimeMillis(); System.out.println("毫秒数:"+now); Date dNow=new Date(now); System.out.println("日期类型:"+dNow); } }
这3例源自http://blog.csdn.net/zhoujian2003/archive/2005/04/22/358363.aspx4 获取系统时期和时间,转换成SQL格式后更新到数据库
(http://blog.csdn.net/netrope/archive/2005/11/19/532729.aspx)
java.util.Date d=new java.util.Date(); //获取当前系统的时间
//格式化日期
new java.text.SimpleDateFormat s= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = s.format(d); //转为字符串
使用RS更新数据库,仍然要用rs.updateString,而不是rs.updateDade。
rs.updateString("regtime",dateStr); //regtime字段为datetime类型的
下面两例源自http://blog.csdn.net/kingter520/archive/2004/10/27/155435.aspx
Date myDate = new Date(); System.out.println(myDate.toLocaleString()); 输出结果为: 2003-5-306 如何格式化小数
DecimalFormat df = new DecimalFormat(",###.00"); double aNumber = 33665448856.6568975; String result = df.format(aNumber); Sytem. out.println(result);
输出结果为:
33,665,448,856.66
其他:获取毫秒时间 System.currentTimeMillis();
(http://blog.csdn.net/zzsxvzzsxv/archive/2007/08/27/1761004.aspx)
定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
输出:
System.out.println(df.format(rs.getDate("bookDate")));
Date date = from.getAfmdate(); if (null != date) { SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); SimpleDateFormat dayFormat = new SimpleDateFormat("dd"); to.setAfmYear(yearFormat.format(date)); to.setAfmMonth(monthFormat.format(date)); to.setAfmDay(dayFormat.format(date)); } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); appPo.setAfmdate(format.parse(appForm.getAfmYear() + "-" + appForm.getAfmMonth() + "-" + appForm.getAfmDay()));
[2] 菜单的容易入门
来源: 互联网 发布时间: 2014-02-18
菜单的简单入门
public class WidgetActivity extends Activity { /** Called when the activity is first created. */ private final String TAG = "WidgetActivity..."; private Menu myMenu; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); myMenu = menu; addRegularMenuItems(menu); addSeconderyMeunItems(menu); return true; } private void addSeconderyMeunItems(Menu menu) { // TODO Auto-generated method stub int base = Menu.CATEGORY_SECONDARY; menu.add(base, base+1, base+1, "sec. item 1"); menu.add(base, base+2, base+2, "sec. item 2"); menu.add(base, base+3, base+3, "sec. item 3"); menu.add(base, base+3, base+3, "sec. item 4"); menu.add(base, base+4, base+4, "sec. item 5"); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub //一下是你的业务逻辑代码 Log.i(TAG, item.getItemId()+ "" ); if(item.getItemId() == 1){ appendText("\nHello..."); }else if(item.getItemId() ==2){ appendText("\nitem2"); } else if( item.getItemId() == 3){ emptyText(); }else if( item.getItemId() == 4){ this.appendMenuItemText(item); this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false); } else if(item.getItemId() == 5 ){ this.appendMenuItemText(item); this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true); } else if( item.getItemId()==6){ this.appendMenuItemText(item); this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true); }else if(item.getItemId() == 7){ this.appendMenuItemText(item); this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false); }else if(item.getItemId() == 8 ){ this.appendMenuItemText(item); this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false); }else if(item.getItemId()==9){ this.appendMenuItemText(item); this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false); } else this.appendMenuItemText(item); return super.onOptionsItemSelected(item); } private void emptyText() { // TODO Auto-generated method stub TextView tv = (TextView) findViewById(R.id.textView); tv.setText(""); } private void appendMenuItemText(MenuItem item) { // TODO Auto-generated method stub String title = item.getTitle().toString(); TextView textView = (TextView) findViewById(R.id.textView); textView.setText(textView.getText() + "\n" + title); } private void appendText(String string) { // TODO Auto-generated method stub TextView tv = (TextView) findViewById(R.id.textView); tv.setText(tv.getText() + string); } private void addRegularMenuItems(Menu menu){ int base = Menu.FIRST; menu.add(base, base, base, "append"); menu.add(base, base+1, base+1, "item2"); menu.add(base, base + 2, base + 2, "clear"); menu.add(base,base+3,base+3,"hide secondery"); menu.add(base, base+4, base+4, "show secomdery"); menu.add(base, base+5, base+5, "enable scondery"); menu.add(base, base+6, base+6, "disable secondery"); menu.add(base, base+7, base+7, "check secondery"); menu.add(base, base+8, base+8, "uncheck secondery"); } }
[3] 创设一个简单的widget(转载自mars)
来源: 互联网 发布时间: 2014-02-18
创建一个简单的widget(转载自mars)
1 、创建一个AppWidgetProvider
public class ExampleAppWidgetProvider extends AppWidgetProvider { //定义一个常量字符串,该常量用于命名Action private static final String UPDATE_ACTION = "mars.appwidget03.UPDATE_APP_WIDGET"; //定义一个常量字符串,用于杀死app private static final String KILL_APP = "com.rui.app.KILL_APP"; @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); } @Override public void onDisabled(Context context) { // TODO Auto-generated method stub super.onDisabled(context); } @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); String action = intent.getAction(); Log.i("TAG", action); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.i("TAG", "==================================="); //创建一个Intent对象 Intent intent = new Intent(); //为Intent对象设置Action intent.setAction(KILL_APP); //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); }
2 、在layout下面创建一个用于显示内容的布局 example_appwidget.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" > <Button android:id="@+id/widgetButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试用按钮" /> </LinearLayout>
3 、再在res下面的xml下面新建一个 xml (example_appwidget_info.xml)文件来指定此widget的一些信息:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="5000" android:initialLayout="@layout/example_appwidget" > </appwidget-provider>
4 、 最后在 AndroidManifest.xml 中配置此widget的一些必须信息:
<receiver android:name="ExampleAppWidgetProvider">
<intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="mars.appwidget03.UPDATE_APP_WIDGET"/> </intent-filter> <intent-filter> <action android:name="com.rui.app.KILL_APP"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver>
最新技术文章: